HP的打印机常常出现漏打的现象,可能和它的自身驱动有关,利用Delphi的VCL Canvas Draw, StretchDraw,
CopyRect, BrushCopy都几乎解决不了问题, 除非打印机驱动程序中已经有监测和处理该错误的功能。
唯一正确的打印图像的方法是利用DIBs(Device Independent Bitmaps),且API打印函数必须正确使用,否则许
多显示驱动程序不能正确填充DIB结构。
下面是核心函数BltTBitmapAsDib(DestDc:hdc;x:word;y:word;
Width:word;Height:word;bm:TBitmap);
它将尽力解决这些问题!
DestDc: 是一个打印画布设备句柄(如
rinter.Canvas.Handle);
x: 图像显示的横坐标;
y: 图像显示的竖坐标;
Width: 图像显示的宽度
Height: 图像显示的长度
bm: 要显示的图像.
BltTBitmapAsDib()函数的具体实现:
uses Printers;
type
PPalEntriesArray = ^TPalEntriesArray;
TPalEntriesArray = array[0..0] of TPaletteEntry;
procedure BltTBitmapAsDib(DestDc:hdc;x:word;y:word;Width:word;Height:word;bm:TBitmap);
var
OriginalWidth :LongInt;
dc : hdc;
IsPaletteDevice : bool;
IsDestPaletteDevice : bool;
BitmapInfoSize : integer;
lpBitmapInfo : PBitmapInfo;
hBm : hBitmap;
hPal : hPalette;
OldPal : hPalette;
hBits : THandle;
pBits : pointer;
lPPalEntriesArray : PPalEntriesArray;
NumPalEntries : integer;
i : integer;
begin
{$IFOPT R+}
{$DEFINE CKRANGE}
{$R-}
{$ENDIF}
OriginalWidth := bm.Width;
dc := GetDc(0);
IsPaletteDevice :=GetDeviceCaps(dc, RASTERCAPS) and RC_PALETTE = RC_PALETTE;
dc := ReleaseDc(0, dc);
if IsPaletteDevice then
BitmapInfoSize := sizeof(TBitmapInfo) + (sizeof(TRGBQUAD) * 255)
else
BitmapInfoSize := sizeof(TBitmapInfo);
GetMem(lpBitmapInfo, BitmapInfoSize);
FillChar(lpBitmapInfo^, BitmapInfoSize, #0);
lpBitmapInfo^.bmiHeader.biSize := sizeof(TBitmapInfoHeader);
lpBitmapInfo^.bmiHeader.biWidth := OriginalWidth;
lpBitmapInfo^.bmiHeader.biHeight := bm.Height;
lpBitmapInfo^.bmiHeader.biPlanes := 1;
if IsPaletteDevice then
lpBitmapInfo^.bmiHeader.biBitCount := 8
else
lpBitmapInfo^.bmiHeader.biBitCount := 24;
lpBitmapInfo^.bmiHeader.biCompression := BI_RGB;
lpBitmapInfo^.bmiHeader.biSizeImage :=((lpBitmapInfo^.bmiHeader.biWidth *longint(lp_
_BitmapInfo^.bmiHeader.biBitCount)) div 8) *lpBitmapInfo^.bmiHeader.biHeight;
lpBitmapInfo^.bmiHeader.biXPelsPerMeter := 0;
lpBitmapInfo^.bmiHeader.biYPelsPerMeter := 0;
if IsPaletteDevice then
begin
lpBitmapInfo^.bmiHeader.biClrUsed := 256;
lpBitmapInfo^.bmiHeader.biClrImportant := 256;
end else
begin
lpBitmapInfo^.bmiHeader.biClrUsed := 0;
lpBitmapInfo^.bmiHeader.biClrImportant := 0;
end;
hBm := bm.ReleaseHandle;
hPal := bm.ReleasePalette;
dc := GetDc(0);
if IsPaletteDevice then
begin
OldPal := SelectPalette(dc, hPal, TRUE);
RealizePalette(dc);
end;
GetDiBits(dc,hBm,0,lpBitmapInfo^.bmiHeader.biHeight,nil,TBitmapInfo(lpBitmapInfo^),
DIB_RGB_COLORS);
hBits :=GlobalAlloc(GMEM_MOVEABLE,lpBitmapInfo^.bmiHeader.biSizeImage);pBits :=
GlobalLock(hBits);
GetDiBits(dc,hBm,0,lpBitmapInfo^.bmiHeader.biHeight,pBits,TBitmapInfo(lpBitmapInfo^),
DIB_RGB_COLORS);
if IsPaletteDevice then
begin
GetMem(lPPalEntriesArray, sizeof(TPaletteEntry) * 256);
{$IFDEF VER100}
NumPalEntries := GetPaletteEntries(hPal,0,256,lPPalEntriesArray^);
{$else
}
NumPalEntries := GetSystemPaletteEntries(dc,0,256,lPPalEntriesArray^);
{$ENDIF}
for i := 0 to (NumPalEntries - 1)do
begin
lpBitmapInfo^.bmiColors
.rgbRed :=lPPalEntriesArray^.peRed;
lpBitmapInfo^.bmiColors.rgbGreen :=lPPalEntriesArray^.peGreen;
lpBitmapInfo^.bmiColors.rgbBlue :=lPPalEntriesArray^.peBlue;
end;
FreeMem(lPPalEntriesArray, sizeof(TPaletteEntry) * 256);
end;
if IsPaletteDevice then
begin
SelectPalette(dc, OldPal, TRUE);
RealizePalette(dc);
end;
dc := ReleaseDc(0, dc);
IsDestPaletteDevice :=GetDeviceCaps(DestDc, RASTERCAPS) and RC_PALETTE = RC_PALETTE;
if IsPaletteDevice then
begin
OldPal := SelectPalette(DestDc, hPal, TRUE);
RealizePalette(DestDc);
end;
StretchDiBits(DestDc,x,y,Width,Height,0,0,OriginalWidth,lpBitmapInfo^.bmiHeader.biHeight,
pBits,lpBitmapInfo^,DIB_RGB_COLORS,SrcCopy);
if IsDestPaletteDevice then
begin
SelectPalette(DestDc, OldPal, TRUE);
RealizePalette(DestDc);
end;
GlobalUnLock(hBits);
GlobalFree(hBits);
FreeMem(lpBitmapInfo, BitmapInfoSize);
bm.Handle := hBm;
bm.Palette := hPal;
{$IFDEF CKRANGE}
{$UNDEF CKRANGE}
{$R+}
{$ENDIF}
end;