Jump to content
grantful

Load image from splite blob field

Recommended Posts

I am trying to load a image from a sqlite database field

tfdQuery sql is 

 

select camarapic from photos

 

Procedure LoadBitmapFromBlob(Bitmap: TBitmap; Blob: TBlobField);
var
  ms, ms2: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    Blob.SaveToStream(ms);
    ms.Position := 0;
    Bitmap.LoadFromStream(ms);
  finally
    ms.Free;
  end;
end;


procedure TCustomerInfo.SpeedButton6Click(Sender: TObject);

var
  bmp: TBitmap;
begin
  bmp := TBitmap.Create;
  try
    //datamodule2.InsertImg.SQL:=('select camarapic form photos where cusid : 2');
    LoadBitmapFromBlob(bmp, TBlobField(datamodule2.InsertImg.FieldByName('camarapic')));
    Image2.Bitmap.Assign(bmp);

  finally
    bmp.Free;


 end;
end;

When i run the app the image2  just goes blank

 

blobissue.thumb.jpg.8913c911a1a49f79857205fb2f4ed152.jpg

 

Thanks for any help.

Share this post


Link to post

Try to find the exact issue by tracing actions. Here you have : querying data from DB, loading BLOB to stream, loading from stream to BMP, loading BMP to image. Check which one of them fails

Edited by Fr0sT.Brutal

Share this post


Link to post
14 hours ago, grantful said:

I am trying to load a image from a sqlite database field

tfdQuery sql is 

 

select camarapic from photos

 


Procedure LoadBitmapFromBlob(Bitmap: TBitmap; Blob: TBlobField);
var
  ms, ms2: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    Blob.SaveToStream(ms);
    ms.Position := 0;
    Bitmap.LoadFromStream(ms);
  finally
    ms.Free;
  end;
end;

When i run the app the image2  just goes blank

 

Are you sure the database BLOB stores the image in the format TBitmap can handle? The code would fail if it is a JPG or GIF or PNG...

Share this post


Link to post

Hi,
The simplest way (without checking errors)

procedure loadbitmapfromblob(abitmap : TBitmap; aBlob : TField);
var aStream : TMemoryStream;
begin
aStream:=TMemoryStream.Create;
try
  TBlobField(aBlob).SaveToStream(aStream);
 // aStream.Position:=0;
  aBitmap.LoadFromStream(aStream);
finally
  aStream.Free;
end;
end;

usage
 

loadbitmapfromblob(Image1.Bitmap,datamodule2.InsertImg.FieldByName('camarapic'));

But can I suggest this ? (FMX program)  or even using Livebindings for "no code"

procedure loadImagefromblob(aImage: TImage; aBlob: TField);
begin
var
  aStream: TMemoryStream;
begin
  if aBlob.IsNull then
    aImage.Visible := False
  else
  begin
    aStream := TMemoryStream.Create;
    try
      TBlobField(aBlob).SaveToStream(aStream);
      try
        aImage.Bitmap.LoadFromStream(aStream);
        aImage.Visible:=true;
      except
        aImage.Visible := False;
      end;
    finally
        aStream.Free;
    end;
  end;
end;
end;

image.thumb.png.796eb218822d78376e328bd116a23319.png

 

for VCL use this procedure, not forgetting to declare units   Vcl.Imaging.jpeg and Vcl.Imaging.pngimage

procedure TFormVCL.loadImage(aImage: Timage; aField: TField);
var aStream : TmemoryStream;
begin
if aField.IsNull then aImage.Visible:=false
else begin
 aStream:=TmemoryStream.Create;
 try
   TBlobField(aField).SaveToStream(aStream);
   aStream.Position:=0;
   AImage.Picture.LoadFromStream(aStream);
   aImage.Visible:=true;
 finally
   aStream.Free;
 end;
end;
end;

image.thumb.png.9a4164a5f2426915a4186151c4e3d89c.png

Edited by Serge_G
Add VCL
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×