How to free images loaded from resource?

Hi,

how to free bitmap images loaded from resource. I try image1.picture.assign(nil), deleteobject(image1.picture.bitmap.handle) but it free only 50% of occupied memory. If I load image from file (image1.LoadFormFile) it work correct but no if I load it form resource.
Any idea?
mmaximAsked:
Who is Participating?
 
EpsylonConnect With a Mentor Commented:
I think that loading the resource causes the filesystem to cache it. That's why only the first time clicking the button memory usage increases.
0
 
ZifNabCommented:
What about image1.Free?

Or am I misinterpreting this?

Can you show us a little more code?

Zif.
0
 
ZifNabCommented:
mmm, have you freed the resource ? eg

{unlock and free the resource}
  UnLockResource(hRes);
  FreeResource(hRes);

Zif.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ZifNabCommented:
without some source-code, I don't know what is going wrong... so guessing... how does your code look?

here example for JPEG's :

{Link the res file}
{$R FOO.RES}

uses Jpeg;

procedure LoadJPEGFromRes(TheJPEG : string;
                          ThePicture : TPicture);
var
  ResHandle : THandle;
  MemHandle : THandle;
  MemStream : TMemoryStream;
  ResPtr    : PByte;
  ResSize   : Longint;
  JPEGImage : TJPEGImage;
begin
  ResHandle := FindResource(hInstance, PChar(TheJPEG), 'JPEG');
  MemHandle := LoadResource(hInstance, ResHandle);
  ResPtr    := LockResource(MemHandle);
  MemStream := TMemoryStream.Create;
  JPEGImage := TJPEGImage.Create;
  ResSize := SizeOfResource(hInstance, ResHandle);
  MemStream.SetSize(ResSize);
  MemStream.Write(ResPtr^, ResSize);
  FreeResource(MemHandle);
  MemStream.Seek(0, 0);
  JPEGImage.LoadFromStream(MemStream);
  ThePicture.Assign(JPEGImage);
  JPEGImage.Free;
  MemStream.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  LoadJPEGFromRes('MYJPEG', Image1.Picture);
end;

Regards, Zif.
0
 
TheNeilCommented:
Why not try loading the resourcfe into a TBitmap, drawing that onto the TImage and then destroying the TBitmap?

The Neil
0
 
mmaximAuthor Commented:

{$R *.DFM}
{$R PICTURES.RES}

procedure TForm1.Button1Click(Sender: TObject);
begin
image1.Picture.Assign(nil);
image1.Picture.Bitmap.LoadFromResourceName(hInstance,'PICTURE_1');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
image1.Picture.Assign(nil);
image1.Picture.Bitmap.LoadFromResourceName(hInstance,'PICTURE_2';
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
image1.Picture.Assign(nil);
image1.Picture.Bitmap.LoadFromResourceName(hInstance,'PICTURE_3;
end;

....
.... etc.

when I click on button1, button2...to display image memory increase according as size of image. If I don't use image1.picture.assign(nil) it increase for double size of image. However, if I use image1.loadformfile function occupied memory is constant.
I try to use TBitmap, assign it to image1 and free but it still don't work.
0
 
EpsylonCommented:
What happens with the memory when you click button1 twice?
0
 
mmaximAuthor Commented:
Only first time when buton is clicked memory increase and image stay somewhere cached in memory.
0
 
mmaximAuthor Commented:
You may try this...

procedure TForm1.Button1Click(Sender: TObject);
Var T : TBitmap;
begin
T := TBitmap.Create;
T.LoadFromResourceID(HINSTANCE,101);
T.Free;

....and memory don't free?!
0
 
k6__Commented:
try this:
Image1.Picture.Bitmap.FreeImage;

This should free the Image & Memory =)

0
 
k6__Commented:
i ment it would Clear the Bitmap & Free
Memory.. It Doesn't Destroys TImage!
0
 
mmaximAuthor Commented:
I am sorry, but this work only when image is loaded from file. Before this, I can use Bitmap.Dormant procedure to free GDI resources and image is created as memory stream. I don't know why image don't free fully but it seems to be that when I load image from resurce two bitmaps are created.
Thanks for effort and sorry for bad english;)
0
 
PeterLarsenCommented:
Try use TPicture.
I have notice that sometimes the bitmap header wont be removed from memory if i use TBitmap.
-dont know why - just a hint..

/Peter
0
 
EpsylonCommented:
I still think it's something else than TBitmap or TPicture that is using the memory.....
0
 
PeterLarsenCommented:
Post a question to Borland. Maybe thay have the answer to this !!
/PL
0
 
mmaximAuthor Commented:
I consulting some experts and they told me looking the Task Manager,Wmem or some other memory utility programs isn't
so relevant for looking memory usage of specific application. System maybe keep this catched some time for later faster loading.
Only relevant function for measuring memory usage is GetHeapStatus.TotalAllocated which tell me how memory is allocated for my application. It is the best to use this function from code, for example:

procedure TForm1.Button1Click(Sender: TObject);
var m : TBitmap;
mem : integer;
begin
Mem := GetHeapStatus.TotalAllocated;
m := TBitmap.Create;
m.LoadFromResourceID(hinstance,101);
m.free;
if GetHeapStatus.TotalAllocated <> Mem then
raise Exception.Create('Memory isn't free');
end;

I wont to say thanks to everybody who is trying to help me to solve this problem.

Max.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.