Jump to content
Behdadsoft

Load Binary Image From Access DB and show in TImage

Recommended Posts

Hi. 

I added many images into Access Database as binary. Now I want to load them form database and put in TImage.

I wrote code below but it doesn't work and give me an error (Attached). 

I compound my code with this example: Streaming Bitmaps and other Binary Data to BLOB Fields

procedure LoadImageFromBinary;
  var
   Image : TJpegImage;
   Stream: TStream;
   query : string;
   Blobfield: TField;
Begin
     query := 'Select JPEG From Table Where Name=MyName';
     Try
        Image := TJpegImage.Create;
         With ADOQuery do
            Begin
              Try
                 SQL.Clear;
                 SQL.Add(query);
                 Open;
                 Blobfield := FieldbyName('JPEG');
                 Stream := CreateBlobStream(Blobfield,bmReadWrite);
                 Image.LoadFromStream(Stream);
                 TImage1.Picture.Assign(Image);
              Finally
                 Close;
              End;
         End;
     Finally
        Stream.Free;
        Image.Free;
     End;
End;

 

00.png

Edited by Behdadsoft

Share this post


Link to post

I Initialized Stream variable before first Try-Finally and now it give me new error:  JPEG error #53.

 

Stream := nil;

 

I searched in the web and couldn't fix this error and tested many solution for this error but none of them worked.

Share this post


Link to post
9 hours ago, Behdadsoft said:

Hi. 

I added many images into Access Database as binary. Now I want to load them form database and put in TImage.

I wrote code below but it doesn't work and give me an error (Attached). 

I compound my code with this example: Streaming Bitmaps and other Binary Data to BLOB Fields

 

 

The source of your problem is probably the line

 TImage1.Picture.Assign(Image);

Should'nt that be

 Image1.Picture.Assign(Image);

 

Share this post


Link to post
On 4/29/2022 at 5:05 PM, Behdadsoft said:

I added many images into Access Database as binary.

Are you sure all of the images are JPEGs?

On 4/29/2022 at 5:05 PM, Behdadsoft said:

I wrote code below but it doesn't work and give me an error (Attached). 

Try this instead:

procedure LoadImageFromBinary;
var
  Image : TJpegImage;
  Stream: TStream;
  Blobfield: TField;
begin
  ADOQuery.SQL.Text := 'Select JPEG From Table Where Name=MyName';
  ADOQuery.Open;
  try
    ADOQuery.First;
    if not ADOQuery.Eof then
    begin
      Blobfield := ADOQuery.FieldbyName('JPEG');
      Stream := ADOQuery.CreateBlobStream(Blobfield, bmRead);
      try
        Image := TJPEGImage.Create;
        try
          Image.LoadFromStream(Stream);
          Image1.Picture.Assign(Image);
        finally
          Image.Free;
        end;
      finally
        Stream.Free;
      end;
    else
      Image1.Picture.Assign(nil);
    end;
  finally
    ADOQuery.Close;
  end;
end;

 

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

×