Brandon Gunning
Brandon Gunning

Reputation: 11

Loading a JPEG image from an Access Database into a TDBImage component

I'm trying to link a database image to a TDBImage component in delphi 10 but it keeps giving me the error that my specified field cannot be found even though there aren't any syntax errors. This is the code I'm using.

function TForm1.JPEGStartBlob(fPic: TBlobField): integer;
var
  bS: TADOBlobStream;
  buffer: Word;
  hx: string;
begin
  Result := -1;

  bS := TADOBlobStream.Create(fPic, bmRead);

  try
    while (Result = -1) and (bS.Position + 1 < bS.Size) do
    begin
      bS.ReadBuffer(buffer, 1);
      hx := IntToHex(buffer, 2);
      if hx = 'FF' then
      begin
        bS.ReadBuffer(buffer, 1);
        hx := IntToHex(buffer, 2);
        if hx = 'D8' then
          Result := bS.Position - 2
        else if hx = 'FF' then
          bS.Position := bS.Position - 1;
      end;
    end;
  finally
    bS.Free;
  end;
end;

procedure TForm1.ShowImage(Sender: TObject);
var
 bsImage : TADOBlobStream;
 jImage : TJPEGImage;
begin
  bsImage := TADOBlobStream.Create(adoLodgeI.FieldByName('Image') // this is the field that can't be 
                                                                 // found
      AS TBlobField, bmRead);

  try
    bsImage.Seek(JPEGStartBlob(adoLodgeI.FieldByName('Image') AS TBlobField),
      soFromBeginning);
    jImage := TJPEGImage.Create;
    try
      jImage.LoadFromStream(bsImage);
      dbiLodge1.Picture.Graphic := jImage;
    finally
      jImage.Free;
    end;
  finally
    bsImage.Free;
  end;
end;

If anyone can help it will be much appreciated.

Upvotes: 1

Views: 300

Answers (1)

Ilyes
Ilyes

Reputation: 14928

You can use TWICImage and then you can just assign it a the TDBImage.Picture directly

Var
  AStream: TMemoryStream;
  APic: TWICImage;
begin
  AStream := TMemoryStream.Create;
  try
    // Here "Data" is a BlobField
    AStream:= TMemoryStream(TPics.CreateBlobStream(TPics.FieldByName('Data'), bmRead));
    AStream.Position:= 0;
    APic := TWICImage.Create;
    try
      APic.LoadFromStream(AStream);
      DBImage1.Picture.Assign(APic);
    finally
      APic.Free;
    end;
  finally
    AStream.Free;
  end;
end;

Works with *.jpeg;*.jpg;*.png;*.bmp;*.ico images and you don't need to worry about if the image is a TJPEGImage or not.

Upvotes: 1

Related Questions