BMP图像的打印的确存在这种问题,而从严格意义上来说,DELPHI是无法完成对DDB图像
正确打印的;最好是利用DIB来实现。
type
PPalEntriesArray = ^TPalEntriesArray;
{for palette re-construction}
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(lpBitmapInfo^.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;
procedure TForm1.Button1Click(Sender: TObject);
var
ScaleX,ScaleY,Wid,Hei :Integer;
begin
if PrintDialog1.Execute then
begin
Printer.begin
Doc;
ScaleX :=GetDeviceCaps(Printer.Canvas.Handle,LogPixelsX) div PixelsPerInch;
ScaleY :=GetDeviceCaps(Printer.Canvas.Handle,LogPixelsY) div PixelsPerInch;
Wid :=Image1.Picture.Bitmap.Width*ScaleX;
Hei :=Image1.Picture.Bitmap.Height*ScaleY;
BltTBitmapAsDib(Printer.Canvas.Handle,
0,
0,
Wid,
Hei,
Image1.Picture.Bitmap);
Printer.EndDoc;
end;
end;
这样是可以实现灰度效果的;
但是边距以及按显示大小打印,需要自己再修改了。