想用TPrinter打印Image控件上绘出的图形。
procedure TForm1.N2Click(Sender: TObject);//绘图形
begin
with form1.Image1 do
begin
Width:=1900;
Height:=2770;
canvas.Pen.Color:=clred;
canvas.Pen.Width:=20;
canvas.MoveTo(10,10);
canvas.LineTo(1800,2700);
canvas.Pen.Color:=clblack;
canvas.LineTo(1800,10);
end;
end;
procedure TForm1.N5Click(Sender: TObject);//打印 参照Delphi例子改
var
AspectRatio:single;
OutputWidth,OutputHeight:Single;
begin
if form1.PrintDialog1.Execute then
begin
Printer.BeginDoc;
try
OutputWidth:=image1.Picture.Width;
OutputHeight:=image1.Picture.Height;
AspectRatio:=OutputWidth / OutputHeight;
if (OutputWidth<Printer.PageWidth) and (OutputHeight<Printer.PageHeight) then
begin
if OutputWidth<OutputHeight then
begin
OutputHeight:=Printer.PageHeight;
OutputWidth:=OutputHeight * AspectRatio;
end
else
begin
OutputWidth:=Printer.PageWidth;
OutputHeight:=OutputWidth / AspectRatio;
end
end;
if OutputWidth>Printer.PageWidth then
begin
OutputWidth:=Printer.PageWidth;
OutputHeight:=OutputWidth / AspectRatio;
end;
if OutputHeight>Printer.PageHeight then
begin
OutputHeight:=Printer.PageHeight;
OutputWidth:=OutputHeight * AspectRatio;
end;
Printer.Canvas.StretchDraw(Rect(0,0,Trunc(OutputWidth),Trunc(OutputHeight)),Image1.Picture.Graphic);
finally
Printer.EndDoc;
end;
{ printer.BeginDoc;
//printer.Canvas.Draw(0,0,form1.Image1.Picture.Graphic);//㈢ 只能打印出白纸?
printer.Canvas.Draw(0,0,Image1.Picture.Bitmap); //㈢ 只能打印出白纸
printer.EndDoc; }
end;
end;都不行,在HP激光打印机上出白纸!!!急等
procedure TForm1.N2Click(Sender: TObject);//绘图形
begin
with form1.Image1 do
begin
Width:=1900;
Height:=2770;
canvas.Pen.Color:=clred;
canvas.Pen.Width:=20;
canvas.MoveTo(10,10);
canvas.LineTo(1800,2700);
canvas.Pen.Color:=clblack;
canvas.LineTo(1800,10);
end;
end;
procedure TForm1.N5Click(Sender: TObject);//打印 参照Delphi例子改
var
AspectRatio:single;
OutputWidth,OutputHeight:Single;
begin
if form1.PrintDialog1.Execute then
begin
Printer.BeginDoc;
try
OutputWidth:=image1.Picture.Width;
OutputHeight:=image1.Picture.Height;
AspectRatio:=OutputWidth / OutputHeight;
if (OutputWidth<Printer.PageWidth) and (OutputHeight<Printer.PageHeight) then
begin
if OutputWidth<OutputHeight then
begin
OutputHeight:=Printer.PageHeight;
OutputWidth:=OutputHeight * AspectRatio;
end
else
begin
OutputWidth:=Printer.PageWidth;
OutputHeight:=OutputWidth / AspectRatio;
end
end;
if OutputWidth>Printer.PageWidth then
begin
OutputWidth:=Printer.PageWidth;
OutputHeight:=OutputWidth / AspectRatio;
end;
if OutputHeight>Printer.PageHeight then
begin
OutputHeight:=Printer.PageHeight;
OutputWidth:=OutputHeight * AspectRatio;
end;
Printer.Canvas.StretchDraw(Rect(0,0,Trunc(OutputWidth),Trunc(OutputHeight)),Image1.Picture.Graphic);
finally
Printer.EndDoc;
end;
{ printer.BeginDoc;
//printer.Canvas.Draw(0,0,form1.Image1.Picture.Graphic);//㈢ 只能打印出白纸?
printer.Canvas.Draw(0,0,Image1.Picture.Bitmap); //㈢ 只能打印出白纸
printer.EndDoc; }
end;
end;都不行,在HP激光打印机上出白纸!!!急等
使用Printer.GetPrinter和Printer.SetPrinter:
var
szDevice, szDriver, szPort: array[0..79] of char;
hDeviceMode: THandle;
pDevMode: PDeviceMode;
begin
Printer.GetPrinter(szDevice, szDriver, szPort, hDeviceMode);
pDevMode := GlobalLock(hDeviceMode);
// 设置pDevMode^.dmPaperSize、pDevMode^.dmPaperLength、pDevMode^.dmPaperWidth等的大小
// 看SDK或MSDN中关于DEVMODE结构的说明
GlobalUnlock(hDeviceMode);
Printer.SetPrinter(szDevice, szDriver, szPort, hDeviceMode);
以下代码经测试可以打印.{我估计是图形大小问题}
procedure TForm1.BtnPringClick(Sender: TObject);
var
strect:Trect; //定义打印输出矩形框的大小
temhi,temwd:integer;
begin
if DIGPrint.execute then
begin
temhi:=imgpic.picture.height;
temwd:=imgpic.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,imgpic.picture.graphic);
enddoc;
end;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if DLGOpen.Execute then
begin
IMGPic.Picture.LoadFromFile(DlgOpen.FileName);
end;
end;procedure TForm1.BtnCloseClick(Sender: TObject);
begin
Close;
end;end.
在with form1.Image1 do
begin
下加上:
picture.bitmap.PixelFormat:=pf24bit; //可以是f1bit, pf4bit, pf8bit, pf15bit, pf16bit, pf24bit, pf32bit中任何一个
就可以打印出来了
以下代码经测试可以打印.{我估计是图形大小问题}
procedure TForm1.BtnPringClick(Sender: TObject);
var
strect:Trect; //定义打印输出矩形框的大小
temhi,temwd:integer;
begin
if DIGPrint.execute then
begin
temhi:=imgpic.picture.height;
temwd:=imgpic.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,imgpic.picture.graphic);
enddoc;
end;
end;
end; procedure TForm1.Button1Click(Sender: TObject);
begin
if DLGOpen.Execute then
begin
IMGPic.Picture.LoadFromFile(DlgOpen.FileName);
end;
end; procedure TForm1.BtnCloseClick(Sender: TObject);
begin
Close;
end; end.
我试验了一下,发现如果去掉设置图片尺寸的语句之后就可以正确打印出来。而一旦设置
了尺寸,就啥都没有了——我才意识到,对Image修改尺寸时,Image会释放其内的Picture
从而无法再绘制上任何东西。所以,建议不要改变图片的大小,而是用新的Image对象取代
老的Image对象,这样才能保证可以顺利的画东西上去(也许有其它方案,但我就是这么干
的:P)。procedure TForm1.N2Click(Sender: TObject);
begin
FreeAndNil(Image1);
Image1:=TImage.Create(Self);
with Image1 do
begin
Width:=1900;
Height:=2770;
Left:=100;
Top:=80;
Parent:=Self;
canvas.Pen.Color:=clred;
canvas.Pen.Width:=20;
canvas.MoveTo(10,10);
canvas.LineTo(1800,2700);
canvas.Pen.Color:=clblack;
canvas.LineTo(1800,10);
end;
end;——通过测试[:D]
窗口没有绘制的线条,而是ScrollBox的底色。怎么回事?creation-zy:“发现如果去掉设置图片尺寸的语句之后就可以正确打印出来。而一旦设置了尺寸,就啥都没有了——我才意识到,对Image修改尺寸时,Image会释放其内的Picture从而无法再绘制上任何东西。所以,建议不要改变图片的大小,”
我仔细试验了,发现可以修改Image的尺寸,只要不超过屏幕的当前分辨率【但不可超过屏幕自身的最佳分辨率,否则是虚拟屏幕分辨率,也出错】,例如:我的屏幕1280*800 ,
程序窗体800*600 ScrollBox窗体792*546 Image窗体788*542 ;N2Click代码设置Image尺寸w不大于1200【1280没试】 h不大于700【800没试】时,打印结果符合预期。
超出则打印白纸。如果设屏幕分辨率更高【超出我的液晶显示器】仍不能骗过。就上述现象,谁能分析下原因。
var
R: TRect;
begin
Printer.BeginDoc;
try
R := Rect(0,0,Image1.Picture.Width,Image1.Picture.Height);//请先在Image1里面载入图片。
Printer.Canvas.StretchDraw(R, Image1.Picture.Graphic);
finally
Printer.EndDoc;
end;
end;
2.如果图片的长宽至少有一个大于纸张,则锁定比例缩放打印; 如果你第1条也需要缩放,注释掉代码的第一个If就行。下面是完整代码;
procedure TForm8.btn_printClick(Sender: TObject);
var w, h, ratePrint, rateImage: integer; dpiRate: double;
begin
if PrintDialog1.Execute then
begin
if printer.PrinterIndex = -1 then
exit;
try
ratePrint := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
rateImage := GetDeviceCaps(Image1.Canvas.Handle, LOGPIXELSX);
dpiRate := ratePrint/ rateImage;
if (Image1.Picture.Graphic.Width * dpiRate < printer.PageWidth) and (Image1.Picture.Graphic.Height * dpiRate < printer.PageHeight) then
begin
w := Round(Image1.Picture.Graphic.Width * dpiRate);
h := Round(Image1.Picture.Graphic.Height * dpiRate);
end
else
if (Image1.Picture.Graphic.Width * dpiRate / printer.PageWidth) - (Image1.Picture.Graphic.Height * dpiRate / printer.PageHeight) >= 0 then//按比率缩小
begin
w := printer.PageWidth ;
h := Round(Image1.Picture.Graphic.Height * dpiRate/ (Image1.Picture.Graphic.Width * dpiRate/ printer.PageWidth)) ;
end
else begin
h := printer.PageHeight ;
w := Round(Image1.Picture.Graphic.Width* dpiRate / (Image1.Picture.Graphic.Width * dpiRate / printer.PageHeight)) ;
end;
printer.BeginDoc ;
printer.Canvas.StretchDraw(Rect(0,0, w , h) ,Image1.Picture.Graphic);
printer.EndDoc ;
except
end;
end;
end;
您在Image1中载入一个超过屏幕分辨率的图片并确保Image1控件两侧的滚动条出现,再打印试试可不可以打印出来预期结果?to 【14楼】hotzhu
我再试一下。to 大家:
请看看我的问题,全部搞清楚后很有用的,请看1楼的提问,9、10楼的深入试验。为什么不成功?
你设置image1.Stretch:=true;就根本不会出现滚动条,就一定可以打印!
(开玩笑地说)小学三年级老师就批评我了,回答问题一定要遵照题意,您没听说过吗?
首先感谢您的支持!
Image1是作为用户的操作界面出现的,所以必须出现滚动条,否则用户就看不清了,
我有一点点偷懒,没有做报表,企图直接打印Image1,遇到困难了,请多帮助!
谢谢大家顺着我的思路钻进了“牛角尖”,帮我钻透吧。