I am using Delphi XE2 (Update 3) and GR32. I cannot use TBitmap32.LoadFromStream() to load image data. This throws the following exception:
Project MyApp.exe raised exception class EInvalidGraphic with message 'Bitmap image is not valid'.
code
uses GR32, GifImg, PngImage, Vcl.Graphics; var pic: TBitmap32; bs: TBytesStream; begin bs := TBytesStream.Create(TClientDataSet(cds).FieldByName('filedata').AsBytes); try pic := TBitmap32.Create; try // bs.SaveToFile('c:\delme.png'); // pic.LoadFromFile('c:\delme.png'); pic.LoadFromStream(bs); // <- EInvalidGraphic exception // do something with 'pic' finally FreeAndNil(pic); end; finally FreeAndNil(bs); end; end;
Bypass (s)
If I comment on the code of LoadFromStream() and uncomment the previous two lines, it works - so it can determine the image format when loading from a file. In this example, bs contains a valid PNG image. However, sometimes it can be GIF, JPG, BMP or another graphic format.
I also know that I can use an intermediate object (e.g. TPNGImage , TJPEGImage , etc.), load image data using LoadFromStream() into the intermediate object, and then Assign() it on TBitmap32 . However, it would be better if TBitmap32 could process any type of image without an intermediate object, which, as I thought, could ...
Question
Does anyone know how to use TBitmap32.LoadFromStream() to load an image and automatically recognize the image format without saving the image to the HDD (even temporarily) or using an intermediate object?
source share