gpt4 book ai didi

delphi - 在delphi中存储之前检查图像大小

转载 作者:行者123 更新时间:2023-12-02 06:01:56 25 4
gpt4 key购买 nike

如何在将图像存储到数据库之前检查图像大小。我需要在用户上传图像之前检查图像大小,以避免无法写入数据库的错误。

下面是我当前如何在数据库中插入图像的片段,它工作正常,除了大于我存储在数据库中的字段大小的图像。

数据库中的图像字段为LONGVARBINARY 484物理长度

              dtm.cds.Insert;
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
Image1.Visible := false;
Field := TBlobField(dtm.cds.FieldByName('PICTURE'));
Stream := dtm.cds.CreateBlobStream(field, bmwrite);

JpegImage := TJPEGImage.Create;
try
JpegImage.Assign(Image1.Picture);
JpegImage.SaveToStream(Stream);
finally
Stream.Free;
dtm.cds.Post;
dtm.cds.ApplyUpdates(-1);
end;

最佳答案

TField 有一个 Size属性:

Indicates the size used in the definition of the physical database field for data types that support different sizes.

尝试更多类似这样的事情:

MemStream := TMemoryStream.Create;
try
JpegImage := TJPEGImage.Create;
try
Picture := TPicture.Create;
try
Picture.LoadFromFile(OpenPictureDialog1.FileName);
JpegImage.Assign(Picture.Graphic);
finally
Picture.Free;
end;
JpegImage.SaveToStream(MemStream);
finally
JpegImage.Free;
end;
Field := dtm.cds.FieldByName('PICTURE');
if MemStream.Size > Field.Size then
raise Exception.Create('Image as a JPEG is too large to store!');
dtm.cds.Insert;
try
Stream := dtm.cds.CreateBlobStream(Field, bmWrite);
try
Stream.CopyFrom(MemStream, 0);
finally
Stream.Free;
end;
dtm.cds.Post;
except
dtm.cds.Cancel;
raise;
end;
finally
MemStream.Free;
end;
dtm.cds.ApplyUpdates(-1);

关于delphi - 在delphi中存储之前检查图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49803761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com