一个老外写的例子,效果很好,呵呵
procedure PrintBitmap(Canvas: TCanvas; DestRect: TRect; Bitmap: TBitmap);
var
BitmapHeader: pBitmapInfo;
BitmapImage: POINTER;
HeaderSize: DWORD; // Use DWORD for D3-D5 compatibility
ImageSize: DWORD;
begin
GetDIBSizes(Bitmap.Handle, HeaderSize, ImageSize);
GetMem(BitmapHeader, HeaderSize);
GetMem(BitmapImage, ImageSize);
try
GetDIB(Bitmap.Handle, Bitmap.Palette, BitmapHeader^, BitmapImage^);
StretchDIBits(Canvas.Handle,
DestRect.Left, DestRect.Top, // Destination Origin
DestRect.Right - DestRect.Left, // Destination Width
DestRect.Bottom - DestRect.Top, // Destination Height
0, 0, // Source Origin
Bitmap.Width, Bitmap.Height, // Source Width & Height
BitmapImage,
TBitmapInfo(BitmapHeader^),
DIB_RGB_COLORS,
SRCCOPY)
finally
FreeMem(BitmapHeader);
FreeMem(BitmapImage)
end
end {PrintBitmap};
{-----------------------------------------------------------------------------
Procedure: CenterText
Author: huazai
Date: 15-ʮһÔÂ-2002
Arguments: »ñÈ¡Êä³ö×Ö·ûµÄºáÏòλÖÃ
Result: INTEGER
-----------------------------------------------------------------------------}
function CenterText(s: string): INTEGER;
begin
RESULT := (Printer.PageWidth - Printer.Canvas.TextWidth(s)) div 2
end {CenterText};
procedure PrintFooterTimeStamp(const LeftMargin: INTEGER);
var
s: string;
begin
//Footer
Printer.Canvas.Font.Name := 'Arial';
Printer.Canvas.Brush.Color := clWhite;
Printer.Canvas.Font.Height :=
MulDiv(GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY), 8, 72);
s := FormatDateTime('m/d/yy h:nn', Now);
Printer.Canvas.TextOut(LeftMargin,
Printer.PageHeight - Printer.Canvas.TextHeight('X'),
s);
end {PrinterFooterTimeStamp};
procedure TMainForm.printClick(Sender: TObject);
var
iFromLeftMargin: INTEGER;
iPrintedImageWidth: INTEGER;
jDelta: INTEGER;
jFromTopOfPage: INTEGER;
jPrintedImageHeight: INTEGER;
s: string;
begin
if PrinterSetupDialog1.Execute then
begin
Screen.Cursor := crHourGlass;
try
Printer.BeginDoc;
// Header
Printer.Canvas.Font.Height :=
MulDiv(GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY), 12, 72);
Printer.Canvas.Font.Name := 'Arial';
jDelta := Printer.Canvas.TextHeight('X');
jFromTopOfPage := 3 * jDelta;
s := 'Image Processer';
Printer.Canvas.TextOut(CenterText(s), jFromTopOfPage, s);
// 5th line from top
jFromTopOfPage := 5 * jDelta;
// Image position and size
// 12% left and right margin
iFromLeftMargin := MulDiv(Printer.PageWidth, 12, 100); // 12%
// Set printed bitmap with to be 76% of paper width
iPrintedImageWidth := MulDiv(Printer.PageWidth, 76, 100); // 76%
// Set printed bitmap height to maintain aspect ratio
jPrintedImageHeight := childform.Image1.Picture.Bitmap.Height *
iPrintedImageWidth div
childform.Image1.Picture.Bitmap.Width;
// maintain aspect ratio of bitmap
// Print Image
PrintBitmap(Printer.Canvas,
Rect(iFromLeftMargin, jFromTopOfPage,
iFromLeftMargin + iPrintedImageWidth,
jFromTopOfPage + jPrintedImageHeight),
childform.Image1.Picture.Bitmap);
ShowMessage('Bitmap printed');
PrintFooterTimeStamp(iFromLeftMargin);
Printer.EndDoc
finally
Screen.Cursor := crDefault
end
end
end;