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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With