我的应用程序是在 Delphi 6 中开发的。由于后台处理和大量数据(它消耗大约 60MB - 120MB 物理内存),这是一个资源密集型应用程序。该应用程序的功能之一是在进行某些处理后创建条形码图像。
如果用户继续生成条形码,那么至少十分之一的条形码会丢失行。
我们有以下步骤生成输出:
- 在内存中创建条形码图像(TImage)。图像的高度为 10 像素。我们使用 pf24bit 像素格式。
- 根据打印机画布调整内存中图像的大小并将其传递到打印机画布。
步骤#2的代码如下:
procedure PrintBitmap(ARect:TRect; Bitmap:TBitmap);
var
Info: PBitmapInfo;
InfoSize: dword{Integer};
Image: Pointer;
ImageSize: dword{ integer};
iReturn : integer ;
iWidth,iHeight :integer;
begin
try
with Bitmap do
begin
iReturn := 1;
GetDIBSizes( Handle, InfoSize, ImageSize );
GetMem( Info, InfoSize );
try
getMem( Image, ImageSize );
try
GetDIB(Handle, Palette, Info^, Image^);
try
with Info^.bmiHeader do
begin
SetStretchBltMode(Printer.Canvas.handle,HALFTONE);
iReturn := **StretchDIBits**(Printer.Canvas.Handle, ARect.Left, ARect.Top,
ARect.Right - ARect.Left, ARect.Bottom - ARect.Top,
0, 0, biWidth, biHeight, Image, Info^, DIB_RGB_COLORS, SRCCOPY);
end;
except on E:Exception do
begin
gobjLog.pWritetoLog(0,'RptWrks2','PrintBitmap','Exception in StretchDIBits with message '+e.Message);
end;
end
finally
FreeMem(Image, ImageSize);
end;
finally
FreeMem(Info, InfoSize);
end;
end
except on E:Exception do
begin
gobjLog.pWritetoLog(0,'RptWrks2','PrintBitmap','Exception in PrintBitMap with message '+e.Message);
end;
end;
我们检查问题出在步骤#2,因为生成的条形码图像没有任何问题。 (我们注释掉了步骤 #2 并将输出作为 BMP 文件来确认这一点)。
此外,我们尝试了以下解决方法:
- 我们使用 TExcellentImagePrinter 组件来执行调整大小操作。但是,问题并没有解决。
- 我们包含 SETPROCESSWORKINGSETSIZE WinAPI 调用来减少/刷新应用程序使用的当前内存。
- 我们在代码中包含了 Sleep(3000),以便 Windows 能够为图像分配内存。然而,包括睡眠可以降低此错误的发生频率。
您能提供一些建议吗?