代码:
procedure TfrmSignPicture.btnExportSignPictureClick(Sender: TObject);
var
savePath: string;
begin
inherited;
savePath := edtSavePath.Text;
if savePath = '' then savePath := GetTempDirectory;
// 最末位不是反斜杠
if savePath[Length(savePath)] <> '\' then
begin
// 补充反斜杠,因为目录必须是\结尾
savePath := savePath + '\';
end;
with dstMain do
begin
First;
while not Eof do
begin
// 导出图片方法1: 图像有效
{
if not DBImage1.Picture.Graphic.Empty then
begin
DBImage1.Picture.SaveToFile(savePath + dstEmployeeemp_ID.AsString + '.bmp');
end
}
// 导出图片方法2:图像字段不为 NULL
if not dstEmployeeemp_SignPicture.IsNull then
begin
TBlobField(dstEmployee.FieldByName('emp_SignPicture')).SaveToFile(savePath + dstEmployeeemp_ID.AsString + '.bmp');
end;
Next;
end;
end;
edtSavePath.Text := savePath;
end;
在Delphi中,判断图像是否为空有多种方法,具体取决于你使用的图像组件和图像数据的来源。以下是几种常见的判断方法:
1. 检查 TPicture 或 TGraphic 对象
pascal
// 方法1: 检查 Graphic 是否为 nil
if (DBImage1.Picture.Graphic = nil) or
(DBImage1.Picture.Graphic.Empty) then
begin
ShowMessage('图像为空');
Exit;
end;
// 方法2: 检查宽度和高度
if (DBImage1.Picture.Width = 0) or (DBImage1.Picture.Height = 0) then
begin
ShowMessage('图像尺寸为0,可能为空');
Exit;
end;
// 方法3: 更全面的检查函数
function IsPictureEmpty(Picture: TPicture): Boolean;
begin
Result := (Picture.Graphic = nil) or
Picture.Graphic.Empty or
(Picture.Width <= 0) or
(Picture.Height <= 0);
end;
// 使用示例
if IsPictureEmpty(DBImage1.Picture) then
begin
ShowMessage('图像为空,无法保存');
Exit;
end;
2. 检查数据库 BLOB 字段
如果图像数据来自数据库的 BLOB 字段,可以直接检查字段状态:
pascal
// 方法1: 检查字段是否为 NULL
var
ImageField: TField;
begin
ImageField := dstEmployee.FieldByName('YourImageFieldName'); // 替换为实际字段名
if ImageField.IsNull then
begin
ShowMessage('数据库图像字段为空');
Exit;
end;
// 对于 BLOB 字段,还可以检查数据大小
if (ImageField is TBlobField) and (TBlobField(ImageField).BlobSize = 0) then
begin
ShowMessage('BLOB 字段数据大小为0');
Exit;
end;
end;
3. 使用 TStream 检查图像数据
通过流来检查图像数据是否有效:
pascal
function IsImageDataValid(ImageField: TBlobField): Boolean;
var
Stream: TStream;
Buffer: array[0..3] of Byte;
BytesRead: Integer;
begin
Result := False;
if ImageField.IsNull or (ImageField.BlobSize = 0) then
Exit;
Stream := nil;
try
Stream := ImageField.DataSet.CreateBlobStream(ImageField, bmRead);
BytesRead := Stream.Read(Buffer, SizeOf(Buffer));
// 检查是否读取到了数据(简单的有效性检查)
Result := (BytesRead > 0);
finally
Stream.Free;
end;
end;
// 使用示例
var
ImageField: TBlobField;
begin
ImageField := TBlobField(dstEmployee.FieldByName('YourImageFieldName'));
if not IsImageDataValid(ImageField) then
begin
ShowMessage('图像数据无效');
Exit;
end;
end;
4. 完整的保存前检查函数
结合以上方法,创建一个完整的检查函数:
pascal
function CanSaveImage(DBImage: TDBImage; ImageField: TField): Boolean;
begin
Result := False;
// 检查数据集状态
if (DBImage.DataSource = nil) or
(DBImage.DataSource.DataSet = nil) or
DBImage.DataSource.DataSet.IsEmpty then
begin
ShowMessage('数据集为空或未打开');
Exit;
end;
// 检查图像字段
if (ImageField = nil) or ImageField.IsNull then
begin
ShowMessage('图像字段为空');
Exit;
end;
// 检查 BLOB 数据大小(如果是 BLOB 字段)
if (ImageField is TBlobField) and (TBlobField(ImageField).BlobSize = 0) then
begin
ShowMessage('图像数据大小为0');
Exit;
end;
// 检查 TPicture 内容
if (DBImage.Picture.Graphic = nil) or
DBImage.Picture.Graphic.Empty or
(DBImage.Picture.Width <= 0) or
(DBImage.Picture.Height <= 0) then
begin
ShowMessage('图像内容为空或无效');
Exit;
end;
Result := True;
end;
// 使用示例
var
ImageField: TField;
begin
ImageField := dstEmployee.FieldByName('YourImageFieldName');
if not CanSaveImage(DBImage1, ImageField) then
Exit;
// 执行保存操作
try
ForceDirectories('C:\SignPicture\');
DBImage1.Picture.SaveToFile('C:\SignPicture\' + dstEmployeeemp_ID.AsString + '.bmp');
ShowMessage('图像保存成功');
except
on E: Exception do
ShowMessage('保存失败: ' + E.Message);
end;
end;
5. 针对特定图像格式的检查
如果需要检查特定格式的图像数据:
pascal
// 检查是否为有效的 BMP 图像
function IsValidBMPImage(Picture: TPicture): Boolean;
begin
Result := False;
if (Picture.Graphic = nil) or Picture.Graphic.Empty then
Exit;
// 检查是否为 TBitmap 或可以转换为位图
if Picture.Graphic is TBitmap then
begin
Result := (Picture.Width > 0) and (Picture.Height > 0);
end
else
begin
// 尝试创建临时位图来检查转换是否可能
var TempBitmap: TBitmap := TBitmap.Create;
try
try
TempBitmap.Assign(Picture.Graphic);
Result := (TempBitmap.Width > 0) and (TempBitmap.Height > 0);
except
Result := False;
end;
finally
TempBitmap.Free;
end;
end;
end;
这些方法可以根据你的具体需求选择使用。通常建议使用第4种方法中的完整检查函数,因为它涵盖了大多数可能的情况。

672

被折叠的 条评论
为什么被折叠?



