procedure TForm8.btn_printClick(Sender: TObject); var w, h: integer; dpiRate: double; begin if PrintDialog1.Execute then begin if printer.PrinterIndex = -1 then exit; try dpiRate := 300 / 96; //打印分辨率和图片分辨率的比例,可根据实际赋值 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;
var w, h: integer; dpiRate: double;
begin
if PrintDialog1.Execute then
begin
if printer.PrinterIndex = -1 then
exit;
try
dpiRate := 300 / 96; //打印分辨率和图片分辨率的比例,可根据实际赋值
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;
可打印出来之后实际大小也就只有 其中的一半也就是打印出来大小只有7CM*4CM
使用方法:
BmpFile1 :要缩放的原始图文件名
BmpFile2 :缩放并存盘的文件名
w2: 缩放后的图像宽度
h2: 缩放后的图像高度
procedure ResizeBMP(BmpFile1,BmpFile2:String;w2,h2:Integer);
{作用:将位图BmpFIle1调整大小为w,h并存盘于BmpFile2中}
var
Bmp1,Bmp2 :TBitmap;
w1,h1:Integer;
begin
Bmp1 :=TBitmap.Create;
Bmp2 :=TBitmap.Create;
Bmp1.LoadFromFile(BmpFile1);
w1:=Bmp1.Width;
h1:=Bmp1.Height;
Bmp2.Width :=w1*w2 div w1;
Bmp2.Height :=h1*h2 div h1;
SetStretchBltMode(Bmp2.Canvas.Handle,HalfTone);
StretchBlt(Bmp2.Canvas.Handle,0,0,w2,h2,
Bmp1.Canvas.Handle,0,0,w1,h1,SRCCOPY);
Bmp2.SaveToFile(BmpFile2);
Bmp1.Free;
Bmp2.Free;
end;
1.我打印判断了图片的大小,如果图片长宽都小于纸张,为了清晰,按实际大小打印;
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;
加分了呀?呵呵!打印图片非常简单,下面的方法即可实现: uses Printers; procedure TForm1.Button1Click(Sender: TObject);
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;
mwy654321 我以前用的就是你说的这种方法,不过打印的效果我不太满意,不过仍然很感谢谢你,最后还是会给你分的
那建议你用ImageEn这个第三方控件吧,有专门的打印Demo演示文档。支持拉伸或缩小。
不过我不想用太多的三方控件