L
lhdyesok
Unregistered / Unconfirmed
GUEST, unregistred user!
//打印
procedure Tprintform.PrintImage(img:Timage);
var
Wid,Hei :Integer;
begin
setPrintSize();
Wid :=img.Picture.Bitmap.Width;
Hei :=img.Picture.Bitmap.Height;
Printer.BeginDoc;
img.picture.Bitmap.HandleType:=BMDIB;
BltTBitmapAsDib(Printer.Canvas.Handle,
0,
0,
Wid,
Hei,
img.Picture.Bitmap);
Printer.EndDoc;
end;
//设置打印纸的类型和方向
Procedure Tprintform.setPrintSize();
var
Device : array[0..cchDeviceName - 1] of Char;
Driver : array[0..(MAX_PATH -1)] of Char;
Port : array[0..32]of Char;
hDMode : THandle;
pDMode : PDevMode;
begin
Printer.GetPrinter(Device,Driver,Port,hDMode);
// 获 取 打 印 机DevMode 结 构 的 句 柄 值, 存 放 在hDMode 中
if hDMode<> 0 then begin
pDMode := GlobalLock(hDMode);
// 获 取 指 向 打 印 机DevMode 结 构 的// 指 针
if pDMode<> nil then begin
pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
// 如 果 要 将 当 前 打 印 机 纸 张 变 为 自 定 义dmPaperSize 必 须 设 置 成256
// pDMode^.dmPaperLength := round(printerpapersize.h*25.4);
// pDMode^.dmPaperWidth := round(printerpapersize.w*25.4);// width;
pDMode^.dmPaperSize :=PaperSize; //根据驱动设置打印纸的PaperSize
{ pdmode^.dmorientation:=dmorient_landscape;
pdmode^.dmorientation:=dmorient_portrait; //设定打印的方向为纵向或横向}
pdmode^.dmOrientation:=dmorient_portrait; //横向打印机
ResetDC(Printer.Handle,pDMode^);
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;
// 设 置 打 印 机 设 备 环 境 句 柄 的 值
GlobalUnlock(hDMode);
end;
end;
end;
//打印配置
procedure Tprintform.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;
上面是我打印模块的全部代码,为什么打印出来的相片会有很多的色块呢,大家帮帮忙!
当我改用下面这两种方法打印的时候,打印出来的都是空白的纸,我用的打印机是:FUJIFILM CX-400
Printer.BeginDoc;
img.picture.Bitmap.HandleType:=BMDIB;
with printer do
begin
SetStretchBltMode(Canvas.Handle,HalfTone);
stretchblt(Canvas.Handle,0,0,printerpapersize.w,printerpapersize.h,img.Canvas.Handle,
0,0,img.Width,img.Height,srccopy);
end;
Printer.EndDoc;
end;
Printer.BeginDoc;
img.picture.Bitmap.HandleType:=BMDIB;
printer.Canvas.StretchDraw(rect(0,0,printerpapersize.w,printerpapersize.h),img.Picture.Bitmap); Printer.EndDoc;
procedure Tprintform.PrintImage(img:Timage);
var
Wid,Hei :Integer;
begin
setPrintSize();
Wid :=img.Picture.Bitmap.Width;
Hei :=img.Picture.Bitmap.Height;
Printer.BeginDoc;
img.picture.Bitmap.HandleType:=BMDIB;
BltTBitmapAsDib(Printer.Canvas.Handle,
0,
0,
Wid,
Hei,
img.Picture.Bitmap);
Printer.EndDoc;
end;
//设置打印纸的类型和方向
Procedure Tprintform.setPrintSize();
var
Device : array[0..cchDeviceName - 1] of Char;
Driver : array[0..(MAX_PATH -1)] of Char;
Port : array[0..32]of Char;
hDMode : THandle;
pDMode : PDevMode;
begin
Printer.GetPrinter(Device,Driver,Port,hDMode);
// 获 取 打 印 机DevMode 结 构 的 句 柄 值, 存 放 在hDMode 中
if hDMode<> 0 then begin
pDMode := GlobalLock(hDMode);
// 获 取 指 向 打 印 机DevMode 结 构 的// 指 针
if pDMode<> nil then begin
pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
// 如 果 要 将 当 前 打 印 机 纸 张 变 为 自 定 义dmPaperSize 必 须 设 置 成256
// pDMode^.dmPaperLength := round(printerpapersize.h*25.4);
// pDMode^.dmPaperWidth := round(printerpapersize.w*25.4);// width;
pDMode^.dmPaperSize :=PaperSize; //根据驱动设置打印纸的PaperSize
{ pdmode^.dmorientation:=dmorient_landscape;
pdmode^.dmorientation:=dmorient_portrait; //设定打印的方向为纵向或横向}
pdmode^.dmOrientation:=dmorient_portrait; //横向打印机
ResetDC(Printer.Handle,pDMode^);
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;
// 设 置 打 印 机 设 备 环 境 句 柄 的 值
GlobalUnlock(hDMode);
end;
end;
end;
//打印配置
procedure Tprintform.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;
上面是我打印模块的全部代码,为什么打印出来的相片会有很多的色块呢,大家帮帮忙!
当我改用下面这两种方法打印的时候,打印出来的都是空白的纸,我用的打印机是:FUJIFILM CX-400
Printer.BeginDoc;
img.picture.Bitmap.HandleType:=BMDIB;
with printer do
begin
SetStretchBltMode(Canvas.Handle,HalfTone);
stretchblt(Canvas.Handle,0,0,printerpapersize.w,printerpapersize.h,img.Canvas.Handle,
0,0,img.Width,img.Height,srccopy);
end;
Printer.EndDoc;
end;
Printer.BeginDoc;
img.picture.Bitmap.HandleType:=BMDIB;
printer.Canvas.StretchDraw(rect(0,0,printerpapersize.w,printerpapersize.h),img.Picture.Bitmap); Printer.EndDoc;