Board index » delphi » How to display a jpeg stored in BLOB

How to display a jpeg stored in BLOB

I stored some jpegs in a db's BLOB field.

I can not display them with the following method:

proc.. ..btnClick(..);
var s:TBlobStream;
s := TBlobStream.Create( (table1.FieldByName( 'Jpeg' ) as TBlobField ),
bmRead ) ;
 //( table1.FieldByName( 'Jpeg' ) as TBlobField ).SaveToStream( s );
 Image1.Picture.Graphic.LoadFromStream( s );

This is a button's onclick method, and I expirienced the followings:
    At first click I've got an acces violation err, reading nil,
    Second time and the rest I've got a DBError, with 'BLOB already
opened' msg.

Stepping to another field causes the loop to restart...
jpeg unit is added, certainly.

Q: What is the correct way to display a jpeg image (using TImage) stored
in BLOB ?

Thanks: Fulci.


Re:How to display a jpeg stored in BLOB

>  //( table1.FieldByName( 'Jpeg' ) as TBlobField ).SaveToStream( s );

If I uncomment the above line, I don't get acces violation, just the BLOB
already opened error...

Re:How to display a jpeg stored in BLOB

OK. I answer myself.

There were two problems(+1):

After getting data from blob the stream must be rewinded:
s.Seek( 0, soFromBeginning );

Every time loading a JPEG image from a MemoryStream TJPEGImage must be
Image1.Picture.Graphic := TJPEGImage.Create;

Altough every time we allocate memory by calling TJPEGImage.Create, we are
not allowed to call the Free method to this. I though, that if I allocate
memory, then I have to release it. But if I do that, I get a lot of serious
error messages. I think TImage internally frees it.


Other Threads