Solved

How to free images loaded from resource?

Posted on 2000-02-22
16
240 Views
Last Modified: 2010-04-04
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?
0
Comment
Question by:mmaxim
  • 5
  • 3
  • 3
  • +3
16 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 2546819
What about image1.Free?

Or am I misinterpreting this?

Can you show us a little more code?

Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2546831
mmm, have you freed the resource ? eg

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

Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2546842
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
 
LVL 5

Expert Comment

by:TheNeil
ID: 2546843
Why not try loading the resourcfe into a TBitmap, drawing that onto the TImage and then destroying the TBitmap?

The Neil
0
 

Author Comment

by:mmaxim
ID: 2547043

{$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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2547233
What happens with the memory when you click button1 twice?
0
 

Author Comment

by:mmaxim
ID: 2547255
Only first time when buton is clicked memory increase and image stay somewhere cached in memory.
0
 

Author Comment

by:mmaxim
ID: 2547281
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:k6__
ID: 2548147
try this:
Image1.Picture.Bitmap.FreeImage;

This should free the Image & Memory =)

0
 
LVL 2

Expert Comment

by:k6__
ID: 2548154
i ment it would Clear the Bitmap & Free
Memory.. It Doesn't Destroys TImage!
0
 

Author Comment

by:mmaxim
ID: 2548382
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
 
LVL 13

Accepted Solution

by:
Epsylon earned 150 total points
ID: 2548524
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
 
LVL 2

Expert Comment

by:PeterLarsen
ID: 2549920
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2554138
I still think it's something else than TBitmap or TPicture that is using the memory.....
0
 
LVL 2

Expert Comment

by:PeterLarsen
ID: 2557102
Post a question to Borland. Maybe thay have the answer to this !!
/PL
0
 

Author Comment

by:mmaxim
ID: 2557465
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now