halocedark
halocedark

Reputation: 71

Delphi: How to print multiple images in one page using Printer?

I cannot seem to figure out how to print multiple images in one page using Printer, I want to display images side by side like this:

enter image description here

But the problem is the images always display in full page like this:

enter image description here

I have this code:

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;

I want to the printer to printout images side by side, how can I accomplish that? Can any one help me please?

Update:

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;

Now it displays the images like sticking at each other, an I want them to display with a little margin between them:

This is when I add margins:

enter image description here

This is without margins:

enter image description here

Upvotes: 0

Views: 900

Answers (1)

AmigoJack
AmigoJack

Reputation: 6174

You have to halfway understand your code, then it will all become obvious. Canvas and Rect are literally just that - and if you max your rectangle out by scale you'll never put two pictures side by side. Cut the values in half and use understand the parameters of functions - I'll use more variables to make it more obvious why your approach is very easy to solve:

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;

This example presumes Bitmap1 and Bitmap2 have similar dimensions.

Upvotes: 1

Related Questions