Printing real dimensions of an image

后端 未结 3 1962
半阙折子戏
半阙折子戏 2021-01-06 21:23

Hi mates i want to print a picture i generated i use the following code

  Printer.BeginDoc;
  Printer.Canvas.Draw(0,0,img1.Picture.Bitmap);
  Printer.EndDoc         


        
3条回答
  •  难免孤独
    2021-01-06 21:26

    You should achieve better results if you resize the image to an intermediate bitmap (with a size suitable for the printer resolution) using one of the resamplers in JCL or Graphics32 and then you print the resized bitmap.

    The following routine will try to get the same size in printer as in the screen:

    uses
      JclGraphics;
    
    procedure PrintGraphic(source: TGraphic);
    var
      dest: TBitmap;
      destWidth, destHeight,
      printerPixelsPerInch_X,  printerPixelsPerInch_Y,
      printerLeftMargin, printerTopMargin: integer;
    begin
      printerPixelsPerInch_X := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
      printerPixelsPerInch_Y := GetDeviceCaps(Printer.Handle, LOGPIXELSY);
      printerLeftMargin      := GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX);
      printerTopMargin       := GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY);
    
      dest := TBitmap.Create;
      try
        destWidth  := source.Width  * printerPixelsPerInch_X div Screen.PixelsPerInch;
        destHeight := source.Height * printerPixelsPerInch_Y div Screen.PixelsPerInch;
    
        Stretch(destWidth, destHeight, rfLanczos3, 0, source, dest);
    
        Printer.BeginDoc;
        try
          Printer.Canvas.Draw(printerLeftMargin, printerTopMargin, dest);
          Printer.EndDoc;
        except
          Printer.Abort;
          raise;
        end;
      finally
        dest.Free;
      end;
    end;
    
    procedure TFormMain.Button1Click(Sender: TObject);
    begin
      if not PrinterSetupDialog.Execute then
        exit;
    
      PrintGraphic(Image1.Picture.Graphic);
    end;
    

提交回复
热议问题