grantful 3 Posted July 12, 2023 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 Thanks for any help. Share this post Link to post
Fr0sT.Brutal 900 Posted July 12, 2023 (edited) 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 July 12, 2023 by Fr0sT.Brutal Share this post Link to post
PeterBelow 238 Posted July 12, 2023 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
grantful 3 Posted July 13, 2023 Thanks for the comments . I will look into to this more. Share this post Link to post
grantful 3 Posted July 13, 2023 I am following this tutorial https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Taking_Pictures_Using_FireMonkey_Interfaces I am not sure about it saveing as a bitmap. Share this post Link to post
Serge_G 87 Posted July 13, 2023 (edited) 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; 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; Edited July 13, 2023 by Serge_G Add VCL 1 Share this post Link to post
grantful 3 Posted July 13, 2023 Thanks so much for the help. It works great Share this post Link to post