My application was developed in Delphi 6. This is a resource-intensive application due to background processing and a large amount of data (it consumes about 60 MB - 120 MB of physical memory). One of the functions of this application is the creation of barcode images after performing a certain process. If the user continues to generate barcodes, then at least one out of ten barcodes has missing lines. We have the following steps to generate the output:
- Create a barcode image (TImage) in memory. Image height - 10 pixels. We use the pf24bit pixel format.
- Resize the image in memory according to the canvas of the printer and transfer it to the canvas of the printer. The code for step # 2 is as follows:
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;
We checked that the problem is in step # 2, since the barcode image is generated without any problems. (We commented on Step # 2 and concluded in the form of BMP files to confirm this).
In addition, we tried the following workarounds:
- We used the TExcellentImagePrinter component to perform the resize operation. But the problem has not been resolved.
- We included the WinAPI call SETPROCESSWORKINGSETSIZE to reduce / update the current memory used by the application.
- We included Sleep (3000) in the code so that Windows could allocate memory for the image. Enabling Sleep, however, reduced the frequency of this error.
Can you provide any suggestions?