Delphi:如何使用打印机在一页中打印多个图像?

Delphi:如何使用打印机在一页中打印多个图像?,delphi,Delphi,我似乎不知道如何使用打印机在一页中打印多个图像, 我想像这样并排显示图像: 但问题是图像总是以整页显示,如下所示: 我有以下代码: procedure TForm1.Button1Click(Sender: TObject); var MyRect: TRect; scale: Double; Bitmap : TBitmap; i: integer; begin try Bitmap := TBitmap.Create;

我似乎不知道如何使用打印机在一页中打印多个图像, 我想像这样并排显示图像:

但问题是图像总是以整页显示,如下所示:

我有以下代码:

procedure TForm1.Button1Click(Sender: TObject);
var
    MyRect: TRect;
    scale: Double;
    Bitmap : TBitmap;
    i: integer;
begin

    try
      Bitmap := TBitmap.Create;

      Bitmap.Width := Image1.Picture.Width;
      Bitmap.Height := Image1.Picture.Height;
      Bitmap.Canvas.Draw(0,0,Image1.Picture.Graphic);

    if PrintDialog1.Execute then
      begin
        if Printer.Orientation = poPortrait then
           scale := GetDeviceCaps(Printer.Handle, LOGPIXELSX) / Screen.PixelsPerInch
        else
           scale := GetDeviceCaps(Printer.Handle, LOGPIXELSY) / Screen.pixelsperinch;

        Printer.BeginDoc;

        MyRect := Rect(0,0, trunc(Bitmap.Width * scale), trunc(Bitmap.Height * scale));
        Printer.Canvas.StretchDraw(MyRect, Bitmap);

        Printer.EndDoc;
      end;

    finally
      Bitmap.Free;
    end;

end;
我想让打印机并排打印出图像,我如何才能做到这一点? 有人能帮我吗

更新:

procedure TForm1.Button1Click(Sender: TObject);
var
    MyRect: TRect;
    scale: Double;
    Bitmap : TBitmap;
    i, x, y, width, height, img_count: integer;
begin

    Bitmap := TBitmap.Create;
    x := 0;
    y := 0;
    img_count := 3;
    try
      begin

        Bitmap.Width := Image1.Picture.Width;
        Bitmap.Height := Image1.Picture.Height;
        Bitmap.Canvas.Draw(0,0,Image1.Picture.Graphic);

        if PrintDialog1.Execute then
          begin
            if Printer.Orientation = poPortrait then
               scale := GetDeviceCaps(Printer.Handle, LOGPIXELSX) / Screen.PixelsPerInch
            else
               scale := GetDeviceCaps(Printer.Handle, LOGPIXELSY) / Screen.pixelsperinch;

            Printer.BeginDoc;

            for i := 1 to img_count do
              begin
                width := trunc(Bitmap.Width * scale / img_count);
                height := trunc(Bitmap.Height * scale / img_count);
                MyRect := Rect(x, y, width, height);
                Printer.Canvas.StretchDraw(MyRect, Bitmap);
                x := x + width;
              end;

            Printer.EndDoc;
          end;
      end;

    finally
      Bitmap.Free;
    end;

end;
现在,它显示的图像像是互相粘住一样,我希望它们之间有一点空白:

这是我添加页边距的时候:

这是没有边际的:


您必须理解代码的一半,然后它就会变得显而易见
Canvas
Rect
就是这样——如果你按比例最大化你的矩形,你就永远不会把两张图片并排放在一起。将值减半,然后使用“了解函数的参数”-我将使用更多变量,以更清楚地说明为什么您的方法非常容易解决:

var
  x, y, width, height: Integer;
...
begin
...
  Printer.BeginDoc;

  x:= 0;  // Start on top left
  y:= 0;
  width:= trunc( Bitmap1.Width* scale/ 2 );  // Half of the size
  height:= trunc( Bitmap1.Height* scale/ 2 )
  Printer.Canvas.StretchDraw( Rect( x, y, width, height ), Bitmap1 );

  x:= width;  // Continue after picture on the right side
  width:= trunc( Bitmap2.Width* scale/ 2 );  // Again half of the size
  height:= trunc( Bitmap2.Height* scale/ 2 )
  Printer.Canvas.StretchDraw( Rect( x, y, width, height ), Bitmap2 );

  Printer.EndDoc;

此示例假定
位图1
位图2
具有相似的维度。

(1)必须放置
位图:=TBitmap.Create之前尝试
。(2) 您可以在找到解决方案。事实上,我认为您可能已经看到了该页面(因为您的图片与该页面的图片完全匹配),您的矩形始终位于左上角。你需要把它放在你想要打印的地方。您的值比例还需要考虑您需要多少列/行。谢谢,但是如果图像较大,是否有办法调整图像大小,使所有图像以相同大小并排打印眼睛:而不是
2
使用图片的数量。非常感谢,这真的很有帮助。如何在图像之间添加边距,请看我上面的更新。当它解决您的问题时,请接受答案。当你有一个新的问题时,创建一个新的问题,在哪里(再次)你应该显示你迄今为止尝试了什么以及你失败了什么。StackOverflow既不是为了做作业,也不是为了完成项目。