Hi all,
I found this example on the net, and works fine. Following code save the jpeg stream data to the database. In my example 'simaj' is the BLOB field in the mysql table.
But I have a problem. I saved the picture to the database but reading jpeg data is not working. Second code is reading data from blob field to the Image component, but I cant figure it out.
Anyone has idea?
Thanks.
//------------------------------Load Picture to the BLOB data-------------------------
procedure TForm1.Button3Click(Sender: TObject);
var
Jpg: TJpegImage;
Stream: TMemoryStream;
FileExt: string;
GraphType: TGraphType;
begin
if dlgOpenPicture.Execute then
begin
Jpg := nil;
Stream := nil;
try
Stream := TMemoryStream.Create;
FileExt := LowerCase(ExtractFileExt(dlgOpenPicture.FileName));
if (FileExt = '.jpg') or (FileExt = '.jpeg') or (FileExt = '.jpe') then
begin
Jpg := TJpegImage.Create;
Jpg.LoadFromFile(dlgOpenPicture.FileName);
Image1.Picture.Assign(Jpg);
GraphType := gtJpeg;
Stream.Write(GraphType, 1);
Jpg.SaveToStream(Stream);
end;
if (ZTable1.State <> dsEdit) and (ZTable1.State <> dsInsert) then ZTable1.Edit;
Stream.Position := 0;
TBlobField(ZTable1.FieldByName('simaj')).LoadFromStream(Stream);
except
jpg.Free;
Stream.Free;
raise;
end;
jpg.Free;
Stream.Free;
end;
end;
//------------------------------Load Picture to the BLOB data-------------------------
//---------------------------------Reading Stream data to the Image component-----------------------------
procedure TForm1.Image1Click(Sender: TObject);
var
Stream: TMemoryStream;
Jpg: TJpegImage;
begin
Jpg := nil;
Stream := nil;
try
// Create a stream and load the contents of the Blob field
Stream := TMemoryStream.Create;
TBlobField(ZTable1.FieldByName('simaj')).SaveToStream(Stream);
if Stream.Size > 0 then begin
// Create a JPEG image and load it from the stream
Jpg := TJpegImage.Create;
Stream.Position := 0;
Jpg.LoadFromStream(Stream);
// Assign the JEPG image to the Picture property of an Image
Image1.Picture.Assign(Jpg);
end
else
Image1.Picture.Assign(nil);
except
Image1.Picture.Assign(nil);
end;
jpg.Free;
Stream.Free;
end;
//---------------------------------Reading Stream data to the Image component-----------------------------