Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving icons as files 1

Status
Not open for further replies.

csteinhilber

Programmer
Aug 2, 2002
1,291
US
I have a TImage in a D6 project that I assign an icon to via a handle I retrieve via a FileInfo object.

The TImage displays the icon fine.

I would then like to be able to save the icon as a BMP or GIF... and it just isn't working.

Code:
var
  hIconHandle: HIcon;
     :
begin
     :
  if hIconHandle <> 0 then
     Image1.Picture.Icon.Handle := hIconHandle;
     :
  Image1.Picture.SaveToFile('c:\test.bmp');
     :
end;
test.bmp is created... but never contains a well-formed bmp.

I've tried:
Code:
  Image1.Picture.Bitmap.SaveToFile('c:\test.bmp');
  Image1.Picture.Graphic.SaveToFile('c:\test.bmp');
same result.

In the help for TPicture.Icon, I did read:
If Icon is referenced when the TPicture contains a Bitmap or Metafile graphic, the graphic won't be converted. Instead, the original contents of the TPicture are discarded and Icon returns a new, blank icon.

I'm sure that has something to do with it... but I can't figure out how to work around it.

Any insight would be appreciated.







-Carl
 
I create BMP files from Jpegs in one of my applications. This is roughly how I do it. I have not tested this adaptation of the code so there may be some typos. You should be able to adapt it to your requirements.
Code:
var
  jpg: TJpegImage;
  bmp: TBitMap;
  aRect: TRect;
begin
  jpg := TJpegImage.Create;
  bmp := TBitMap.Create;
  try
    jpg.LoadFromFile ( 'test.jpg' );
    bmp.Height := jpg.Height;
    bmp.Width := jpg.Width;
    aRect := Rect ( 0, 0, bmp.Width, bmp.Height );
    bmp.Canvas.StretchDraw ( aRect, jpg );
    bmp.SaveToFile ( 'test.bmp' );
  finally
    jpg.free;
    bmp.free;
  end;
Hopefully this will help.

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top