DMTrump
asked on
Getting a bitmap from Timage into which I've loaded jpg, png, or gif file
I have the following code in a Delphi procedure. It works fine when I load a .bmp file but when I load a jpg, png or gif, although I can properly display the loaded image (by putting the picture into a visible Timage, I can't access the bitmap that I need to pass to my AddBitMap procedure.
Obviously I'm doing something wrong but I've tried everything I can think of to no avail!
Does anyone have the answer that has eluded me?
<snip>
img:=TImage.Create(frmOD);
img.AutoSize:=True;
img.Picture.LoadFromFile(d lgOpenPic1 .FileName) ; // .jpg, .png, .gif, .bmp ??
bm:=TBitmap.Create;
bm.Height:=img.Height;
bm.width:=img.Width;
bm.assign(img.Picture.bitm ap);
_form.AddBitMap(bm);
bm.Free;
img.Free;
</snip>
Obviously I'm doing something wrong but I've tried everything I can think of to no avail!
Does anyone have the answer that has eluded me?
<snip>
img:=TImage.Create(frmOD);
img.AutoSize:=True;
img.Picture.LoadFromFile(d
bm:=TBitmap.Create;
bm.Height:=img.Height;
bm.width:=img.Width;
bm.assign(img.Picture.bitm
_form.AddBitMap(bm);
bm.Free;
img.Free;
</snip>
This is better and cleaner, add code to check the extension if it is JPeg otherwise load the normal way
var
bm: TBitmap;
J: TJPegImage;
img: TImage;
FileExt: string;
begin
img:=TImage.Create(frmOD);
try
FileExt := ExtractFileExt(dlgOpenPic1.FileName);
if Pos('jpg', LowerCase(FileExt)) > 0 then
begin
J := TJPegImage.Create;
try
J.LoadFromFile(dlgOpenPic1.FileName);
img.picture.Graphic.Assign(J);
finally
FreeAndNil(J);
end;
end
else
img.Picture.LoadFromFile(dlgOpenPic1.FileName)
img.AutoSize:=True;
bm:=TBitmap.Create;
try
bm.Height:=img.Height;
bm.width:=img.Width;
bm.assign(img.Picture.bitmap);
_form.AddBitMap(bm);
finally
FreeAndNil(bm);
end;
finally
FreeAndNil(img);
end;
end;
ASKER
Yes I had found that offered as a solution elsewhere - but it's not completely what I want. That would handle it if I determine that I'm loading a jpeg file - but what about when someone loads a Gif or Png?
Look at my second solution, it checks for Jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@RezaSadigh
Good point
ASKER
I've tried your second example and I get a fault on the
img.picture.Graphic.Assign (J);
Any further hints?
img.picture.Graphic.Assign
Any further hints?
ASKER
Also, this would only satisfy for bmp and jpg - what can I do for png and gif?
ASKER
Trying RezaSadigh hint right now
You need to use the bitmap property
img.picture.Bitmap.Assign(
Ok,
something should become clear,
what is your delphi version? TImage.Picture.LoadFromFil e on native form can't load PNG.
Are you using some extra code for doing that.
something should become clear,
what is your delphi version? TImage.Picture.LoadFromFil
Are you using some extra code for doing that.
For GIF and PNG, you will need third party components since these are not provided by Delphi
for GIF take a look at TGIFImage component
http://www.tolderlund.eu/delphi/
I have not worked with PNG images in delphi but you can find a component for that as well
ASKER
Actially I was already using the JPEG unit so all I needed to do was use the bm.assign(img.Picture.Grap hic); suggested by RezaSadigh - however ewangoya's comments were helpful too and I like to share the points when several people have contributed!
Thank you my friends I now have code that works for all file types!
Thank you my friends I now have code that works for all file types!
As I mentioned earlier in #35079407, if you include JPeg, GIFImage in your uses clause in the interface section, you don't need any special method to load the images, just call
img.Picture.LoadFromFile(d
ASKER
Yes, to your hints about components for handling png and gif - I'd done that so long ago on this project that I'd forgotten about it (also of course I was already using the jpeg unit. Everything was working fine until I needed to pur the graphic int a bitmap for another piece of functionality. Thanks again!
Glad to help
ASKER
So just a final clarification:
I already had JPEG, GIFImg and PNGimage in my uses clause The only part that was missing was using the Graphic.Assign(bm) instead of BitMap.assign(bm)
And by the way - I'm currently on Delphi 2007
I already had JPEG, GIFImg and PNGimage in my uses clause The only part that was missing was using the Graphic.Assign(bm) instead of BitMap.assign(bm)
And by the way - I'm currently on Delphi 2007
Best regards
uses
JPeg;
Open in new window