用TPrinter打印Image中的图形(200分)

  • 主题发起人 幼虫2004
  • 开始时间

幼虫2004

Unregistered / Unconfirmed
GUEST, unregistred user!
见我的代码:
procedure TForm1.N2Click(Sender: TObject);//新建图形
begin
with form1.Image1do
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);//打印
var
AspectRatio:single;
OutputWidth,OutputHeight:Single;
begin
//下面改编自delphi例子,在HP激光打印机中出白纸
if form1.PrintDialog1.Execute then
begin
Printer.begin
Doc;
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.begin
Doc;//下面代码改编自大富翁的旧贴,但HP激光打印机只打出白纸
//printer.Canvas.Draw(0,0,form1.Image1.Picture.Graphic;
printer.Canvas.Draw(0,0,Image1.Picture.Bitmap);
printer.EndDoc;}
end;
end;

翻了好多资料,都没解决的方法!!
 
我试验了一下,发现如果去掉设置图片尺寸的语句之后就可以正确打印出来。而一旦设置
了尺寸,就啥都没有了——我才意识到,对Image修改尺寸时,Image会释放其内的Picture
从而无法再绘制上任何东西。所以,建议不要改变图片的大小,而是用新的Image对象取代
老的Image对象,这样才能保证可以顺利的画东西上去(也许有其它方案,但我就是这么干
:p)。
procedure TForm1.N2Click(Sender: TObject);
begin
FreeAndNil(Image1);
Image1:=TImage.Create(Self);
with Image1do
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]
 
creation-zy大哥好
太敬业了,2008-10-9 1:09:52还不睡,注意身体呀,那可不是闹着玩儿的!
我试试先。
 
我试验了creation-zy的代码,procedure TForm1.N2Click(Sender: TObject);//新建图形
的结果是空白。
我的Image1控件放在Form的ScrollBox中。运行后,ScrollBox控件两侧的滚动条出来了,但
窗口没有绘制的线条,而是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没试】时,打印结果符合预期。
超出则打印白纸。如果设屏幕分辨率更高【超出我的液晶显示器】仍不能骗过。
就上述现象,谁能分析下原因。
 
不让灌水,就加点油,顶!![:(!]
 
看来是尺寸的问题,请参考:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=413511
http://www.delphibbs.com/delphibbs/dispq.asp?lid=058796
 
不是尺寸的问题,是TPrint只能打印窗体可见的部分,超出就出此问题。怎么解决?
 
大家来浇油!!!
 
大家来加油!
 
[:(]真冷清,没人理?!
 
顶部