有关画布打印 ( 积分: 100 )

  • 主题发起人 主题发起人 zybsc
  • 开始时间 开始时间
Z

zybsc

Unregistered / Unconfirmed
GUEST, unregistred user!
我在image的画布上画了图形,怎么样将其打印出来呢?谢谢各位!
 
我在image的画布上画了图形,怎么样将其打印出来呢?谢谢各位!
 
用fastreport
 
看看这个???
xuhao1 (2004-06-09 23:22:33)
procedure PrintBitmap(Canvas: TCanvas; DestRect: TRect; Bitmap: TBitmap);
var
BitmapHeader: pBitmapInfo;
BitmapImage: POINTER;
HeaderSize: DWORD;
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,
// 目标初始坐标
DestRect.Right - DestRect.Left,
// 目标宽度
DestRect.Bottom - DestRect.Top,
// 目标高度
0, 0, // 源初始坐标
Bitmap.Width, Bitmap.Height,
// 源高度和宽度
BitmapImage,
TBitmapInfo(BitmapHeader^),
DIB_RGB_COLORS,
SRCCOPY)
//位图按照要求进行拉伸
finally
FreeMem(BitmapHeader);
//内存释放
FreeMem(BitmapImage);
end;
end; {PrintBitmap}
//打印时间

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};



//返回字符串的中间位置

function CenterText(s: string): INTEGER;
begin
RESULT := (Printer.PageWidth - Printer.Canvas.TextWidth(s)) div 2
end {CenterText};


procedure TForm1.Button1Click(Sender: TObject);
var
iFromLeftMargin: INTEGER;
//左边距
iPrintedImageWidth: INTEGER;
//打印位图宽度
jDelta: INTEGER;
jFromTopOfPage, XScale: INTEGER;
//页顶距
jPrintedImageHeight: INTEGER;
//打印图像高度
RealWidth: integer;
//真实尺寸
s: string;
begin
if PrinterSetupDialog1.Execute then
begin
Screen.Cursor := crHourGlass;
try
Printer.BeginDoc;
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);
// 输出位置
jFromTopOfPage := 5 * jDelta;
XScale := GetDeviceCaps(Printer.Handle, LOGPIXELSX) div
Screen.PixelsPerInch;
//获得打印机和显示器的分辨率
RealWidth := XScale * Image1.Picture.Bitmap.Width;
//获得真实尺寸
if RealWidth < Printer.PageWidth then
begin
iFromLeftMargin := (Printer.PageWidth - RealWidth) div 2;
//当真实尺寸小于页面宽度时
iPrintedImageWidth := RealWidth;
end
else
begin
iFromLeftMargin := Printer.PageWidth div 10;
// 大于时则设置边距为页面宽度得1/10
iPrintedImageWidth := 8 * iFromLeftMargin;
end;
jPrintedImageHeight := Image1.Picture.Bitmap.Height *
iPrintedImageWidth div
Image1.Picture.Bitmap.Width;
// 高度也进行相应得调整
// 调用打印位图函数
PrintBitmap(Printer.Canvas,
Rect(iFromLeftMargin, jFromTopOfPage,
iFromLeftMargin + iPrintedImageWidth,
jFromTopOfPage + jPrintedImageHeight),
Image1.Picture.Bitmap);
ShowMessage('Bitmap printed');
PrintFooterTimeStamp(iFromLeftMargin);
//打印时戳
Printer.EndDoc
finally
Screen.Cursor := crDefault
end
end
end;
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2539743
-------------------------------转自网络

procedure TForm1.Button1Click(Sender: TObject);
VAR
strect:Trect; //定义打印输出矩形框的大小
temhi,temwd:integer;
begin
if printdialog1.execute then
begin
temhi:=image1.picture.height;
temwd:=image1.picture.width;
while (temhi<printer.pageheight div 2)and //将图形放大到打印页面的1/2
(temwd<printer.pagewidth div 2) do
begin
temhi:=temhi+temhi;
temwd:=temwd+temwd;
end;
with strect do //定义图形在页面上的中心位置输出
begin
left:=(printer.pagewidth -temwd) div 2;
top:=(printer.pageheight-temhi) div 2;
right:=left+temwd;
bottom:=top+temhi;
end;
with Printer do
begin
begindoc;
//将放大的图形向打印机输出
canvas.stretchdraw(strect,image1.picture.graphic);
enddoc;
end;
end;
end;
 
后退
顶部