Fdmemtable 記憶體表儲存圖片的例子

沧江魅影發表於2024-08-15

var
aStream: TMemoryStream;
LDataSet: TFDDataSet; // 申請一個FD資料集
MyStream: Tmemorystream;
MyJPEG: TJpegImage;
MyPng: TPngImage;
begin
inherited;
if img2.Picture.Graphic = nil then
begin
Application.MessageBox('沒有圖片可以增加!!', '提示', MB_OK + MB_ICONWARNING + MB_TOPMOST);

end
else
begin
aStream := TMemoryStream.Create();
MyStream := TMemoryStream.Create();
try
img2.Picture.SaveToStream(aStream);
fdmtPicture.Append;
fdmtPicture.FieldByName('p_picture_id').AsLargeInt := getSnowflake;
fdmtPicture.FieldByName('p_picture_guid').AsString := GetGUID;
fdmtPicture.FieldByName('p_products_id').AsLargeInt := aid;
fdmtPicture.FieldByName('p_picture_name').AsString := EditImageName.Text;
if img2.Picture.Graphic is TJPEGImage then
begin
MyJPEG := TJpegImage.Create;
MyJPEG.Assign(img2.Picture.Graphic);
MyJPEG.CompressionQuality := 10;
MyJPEG.Compress;
MyJPEG.SaveToStream(MyStream);
end;
if img2.Picture.Graphic is TPngImage then
begin
MyPng := TPngImage.Create;
MyPng.Assign(img2.Picture.Graphic);
MyPng.CompressionLevel := 9;
MyPng.SaveToStream(MyStream);
end;
MyStream.Position := 0;
TBlobField(fdmtPicture.FieldByName('p_picture_thumbnail')).LoadFromStream(MyStream);
aStream.Position := 0;
TBlobField(fdmtPicture.FieldByName('p_picture_image')).LoadFromStream(aStream);
fdmtPicture.Post;
img2.Picture.Graphic := nil;
finally
MyStream.free;
aStream.Free;
end;
end;

轉自https://www.cnblogs.com/Thenext/p/16422021.html

相關文章