Why do I get JPEG error 42 when it's stored in a database?
Asked Answered
G

1

6

I have problem to display my Picture in TImage from my MySql database using Delphi.

I save picture to my database with this code and work Perfectly.

var
  AStream : TMemoryStream;

AStream := TMemoryStream.Create;
  try
    Image1.Picture.Graphic.SaveToStream(AStream);
    AStream.Position := 0;
    if ADODataSet1.Active then
    begin
      ADODataSet1.Edit;
      TBlobField(ADODataSet1.FieldByName('MyField')).LoadFromStream(AStream);
      ADODataSet1.Post;
    end;
  finally
    AStream.Free;
  end;

But, problem is when I want to retrieve that Pictures. I use this code but I can display only first image from my database. I uses this code on DBGrid event - OnDrawColumnCell. AND WHEN I USE THIS CODE I'M GETTING MESSAGE JPEG ERROR #42!

var
  AStream : TMemoryStream;
begin
  AStream := TMemoryStream.Create;
  try
    if ADODataSet1.Active then
    begin
      TBlobField(ADODataSet1.FieldByName('MyField')).SaveToStream(AStream);
      AStream.Position := 0;
      Image1.Picture.Graphic.LoadFromStream(AStream);
    end;
  finally
    AStream.Free;
  end;
end;

Can somebody show me how to Fix This Problem. Thank you.

Guv answered 2/10, 2012 at 20:23 Comment(5)
Do all rows in the dataset definitely contain images?Tetanize
What type of field is 'MyField'?Insula
FOR Andriy M - No, some row will be empty depends do I have picture from that contact or not, but I would like to put a default picture like "Question mark" in TImage for contacts whos dont have a picture. If I put that default picture in TImage everyone will have a picture save in database.Guv
FOR Sertac Akyuz - Field 'MyField' is IMG(LONGBLOB)Guv
As a useful diagnostic, you could throw in a call to: AStream.SaveToFile('c:\somepath\debug.jpg'); I'd put it right before the LoadFromSTream call. That'll show you whether or not you've got a valid image.Lifer
M
9

JPEG error 42 is reported when the stream is truncated. For example, if you attempt to load a zero length file into a TJPEGImage then error 42 is the end result.

If some images display, but not all, then the most likely explanation is that the data is somehow not making the round-trip to the DB and back.

Check the size of the BLOB field when you write it out. Check that it tallies with the size of the file when you write it to a disk file. Check that the disk file is a valid JPEG. Then confirm that the BLOB field has the exact same length when you re-read it. Perhaps there's something wrong with your DB code and the stream is getting truncated.

So, the very first step here is to confirm that you can recover the exact same data that you originally put into the DB.

The only other thought I have is that the graphic in the image control is not always a JPEG. The code that you use to load the image, Image1.Picture.Graphic.LoadFromStream() assumes that the data is a JPEG. If you had saved something other than a JPEG then LoadFromStream() would fail.

Mirador answered 2/10, 2012 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.