Watch, Follow, &
Connect with Us

Welcome, Guest
Guest Settings
Help

Thread: Load Jpeg blob into Bitmap raise exception 'Bitmap image is not valid'



Permlink Replies: 2 - Last Post: Apr 13, 2017 12:29 PM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Adalberto Baldini

Posts: 126
Registered: 1/31/12
Load Jpeg blob into Bitmap raise exception 'Bitmap image is not valid'
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 12, 2017 11:22 PM
Hi, in DXE5 I try to load a blob field containing a Jpeg into a bitmap and get error when I try LoadFromStream ?

var
blob : TStream;
Bmp: TBitmap;
begin

try
blob := IBDts_Fields_Read.CreateBlobStream(
IBDts_Fields_Read.FieldByName('FOTO'), bmread);
blob.Seek(0, soFromBeginning);
try
Bmp := TBitmap.Create;
Bmp.LoadFromStream(blob);
.....
finally
Bmp.Free;
end;
finally
blob.Free ;
end;

Adalberto Baldini
Alex Belo

Posts: 499
Registered: 10/8/06
Re: Load Jpeg blob into Bitmap raise exception 'Bitmap image is not valid'
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 13, 2017 8:35 AM   in response to: Adalberto Baldini in response to: Adalberto Baldini
Adalberto Baldini wrote:

Hi, in DXE5 I try to load a blob field containing a Jpeg into a
bitmap and get error when I try LoadFromStream ?

var
blob : TStream;
Jpg: TJPEGImage
Bmp: TBitmap;
begin
blob := nil;
Jpg:=nil;
BMP:=nil;
try
blob := IBDts_Fields_Read.CreateBlobStream(
IBDts_Fields_Read.FieldByName('FOTO'), bmread);
blob.Seek(0, soFromBeginning);

Jpg := TJPEGImage.Create;
Bmp := TBitmap.Create;
Jpg.LoadFromStream(blob);
Bmp.Assign(Jpg);
end;
finally
blob.Free;
Bmp.Free;
Jpg.Free;
end;

--
Alex
Remy Lebeau (Te...


Posts: 8,201
Registered: 12/23/01
Re: Load Jpeg blob into Bitmap raise exception 'Bitmap image is not valid'
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 13, 2017 12:29 PM   in response to: Adalberto Baldini in response to: Adalberto Baldini
Adalberto wrote:

Hi, in DXE5 I try to load a blob field containing a Jpeg into a bitmap
and get error when I try LoadFromStream ?

In VCL, TBitmap only supports BMP data. You can't load a JPG into TBitmap,
you have to use TJPEGImage instead, which you can then Assign() to TBitmap
if you actually need the image in BMP format:

var
  blob : TStream;
  Bmp: TBitmap;
  Jpg: TJPEGImage;
begin
  Bmp := TBitmap.Create;
  try
    blob := IBDts_Fields_Read.CreateBlobStream(IBDts_Fields_Read.FieldByName('FOTO'), 
bmRead);
    try
      Jpg := TJPEGImage.Create;
      try
        Jpg.LoadFromStream(blob);
        Bmp.Assign(Jpg);
      finally
        Jpg.Free;
      end;
    finally
      blob.Free;
    end;
    ...
  finally
    Bmp.Free;
  end;
end;


In FireMonkey, TBitmap supports multiple image types, including JPG.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02