Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to free images loaded from resource?

Posted on 2000-02-22
16
Medium Priority
?
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

670 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