将图像字段定义为blob字段。根据要存储的图片的大小,选择
TINYBLOB, BLOB, MEDIUMBLOB, or LONGBLOB
((
https://dev.mysql.com/doc/refman/8.0/en/blob.html
要在Blob字段中加载JPEG等图形文件,可以使用以下代码:
procedure TForm1.Button1Click(Sender: TObject);
var
BlobStream: tStream;
Surf: TBitmapSurface;
begin
if Image1 <> nil then
begin
Surf := TBitmapSurface.Create;
Surf.Assign(Image1.Bitmap);
BlobStream := TMemoryStream.Create;
if not TBitmapCodecManager.SaveToStream(BlobStream, Surf, '.jpg') then
raise EBitmapSavingFailed.Create('No');
BlobStream.Position := 0;
myquery2.ParamByName('imagem').LoadFromStream(BlobStream, ftBlob);
myquery2.ExecSQL;
Surf.Free;
end;
end;
要将数据从Blob字段加载到TImage组件,请使用以下代码:
procedure TForm1.Button2Click(Sender: TObject);
var
BlobStream: TStream;
begin
BlobStream := TMemoryStream.Create;
myquery1.GetBlob(myQuery1.FieldByName('imagem')).SaveToStream(BlobStream);
Blobstream.Position := 0;
Image2.bitmap.loadfromstream(blobstream);
BlobStream.Free;
end;