Solved

How to free images loaded from resource?

Posted on 2000-02-22
16
239 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

12 Experts available now in Live!

Get 1:1 Help Now