How to store/retrieve TImage content to a TBlob ?

I am trying to get my head around working with TBlob.

Does anyone have an example for storing/retrieving an image (e.g. bitmap or png) in Aurelius TBlob?

Solved ;-) For example:

      memStream := TMemoryStream.Create;
      Image.Bitmap.SaveToStream( memStream );
      memStream.Position := 0;
      fProduction.Thumbnail.LoadFromStream( memStream );
      memStream.DisposeOf;

Hi



I wrote a helper routine for this, that lets you specify the format in which the image is saved:



procedure Bitmap2BlobAs(aBmp: TBitmap; const aBlob: TBlob; aType: string);

var

bmp: TBitmapSurface;

bs: TBytesStream;

begin

Bmp := TBitmapSurface.create;

try

    bmp.assign(aBmp);

    bs := TBytesStream.Create;

    try

      TBitmapCodecManager.SaveToStream(bs, bmp, aType);

      aBlob.AsBytes := bs.Bytes;

    finally

      bs.free;

    end;

finally

    bmp.Free;

end;

end;



This way you can save the images as Jpg and save space.

Be aware of the fact that a TBlob is a RECORD not a class. So passing it to helpers routines requires it have CONST in parameter specification, otherwise you are passing a copy of the blob to the helper routine and changes you make in it will have no effect (this has caused me some headache in the past;-)).

sorry, I forgot to mention how to call the routine:

   Bitmap2BlobAs( Image, fProduction.Thumbnail, '.jpg' );

Hi Kick,


that's very helpful.

Have you got the opposite helper? Blob2Bitmap?

Thanks

John

of course, this one is much simpeler though, because we don't have to convert



procedure Blob2bitmap(const aBlob: TBlob; aBMP: TBitmap);

var MS: TMemoryStream;

begin

MS := TMemoryStream.Create;

try

    aBlob.SaveToStream(MS);

    MS.Position := 0;

    aBMP.LoadFromStream(MS);

finally

    MS.free;

end;

end;

Hi Kick,


thanks a lot. 

John