1

I'd drawing in image canvas using lineto function ,and then printed that . but the printer creates an empty page .i used printer.canvas.StretchDraw but if the image been saved then load on, image printing would be successful. anybody knows the reason?

1
  • 3
    Could you show the code that is involved? It's easier to help if we see what you're doing. Commented Sep 27, 2009 at 19:46

2 Answers 2

2

Here's what I use:

function PrintWrapper(ADrawProc : TDrawProc;
                      APreview: Boolean;
                      AWidth, AHeight : longint) : boolean;
var
  bmp : Graphics.TBitmap;
  PixPerInchX, PixPerInchY : longint;
begin
  bmp := Graphics.TBitmap.Create;
  try
    if APreview then begin
      PixPerInchX := GetDeviceCaps(bmp.Canvas.Handle, LOGPIXELSX);
      PixPerInchY := GetDeviceCaps(bmp.Canvas.Handle, LOGPIXELSY);
      bmp.Width := PixPerInchX * AWidth;
      bmp.Height := PixPerInchY * AHeight;
      bmp.Canvas.Brush.Color := clWhite;
      bmp.Canvas.FillRect(Rect(0,0,bmp.Width,bmp.Height));
      ADrawProc(bmp.Canvas,AWidth,AHeight);
      result := (not APreview) or TFormImagePrintPreview.PrintNow(bmp);
      if not result then
        exit;
    end;
    Printer.Orientation := poLandscape;
    Printer.BeginDoc;
    try
      ADrawProc(Printer.Canvas,AValues,AWidth,AHeight);
    finally
      Printer.EndDoc;
    end;
    result := true;
  finally
    FreeAndNil(bmp);
  end;
end;

Where DrawProc is:

type
  TDrawProc = procedure(ACanvas : TCanvas;
                        Width, Height : longint);

And the image preview is:

unit formImagePreviewDef;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TformImagePrintPreview = class(TForm)
    pnBottom: TPanel;
    btPrint: TButton;
    btCancel: TButton;
    imgMain: TImage;
    lbNotes: TLabel;
    procedure btPrintClick(Sender: TObject);
    procedure btCancelClick(Sender: TObject);
  public
    procedure Init(AGraphic : TGraphic; ANotes : string = '');
    class function  PrintNow(AGraphic : TGraphic; ANotes : string = '') : boolean;
  end;

implementation

{$R *.dfm}

{ TformImagePrintPreview }

procedure TformImagePrintPreview.btCancelClick(Sender: TObject);
begin
  Close;
  ModalResult := mrCancel;
end;

procedure TformImagePrintPreview.btPrintClick(Sender: TObject);
begin
  Close;
  ModalResult := mrOk;
end;

procedure TformImagePrintPreview.Init(AGraphic: TGraphic; ANotes : string = '');
const
  MAXSIZE = 600;
begin
  lbNotes.caption := ANotes;
  imgMain.Picture.Assign(AGraphic);
  if AGraphic.Height > AGraphic.Width then begin
    ClientWidth := trunc(MAXSIZE * AGraphic.Width / AGraphic.Height);
    ClientHeight := MAXSIZE + pnBottom.Height;
  end else begin
    Width := MAXSIZE;
    Height := trunc(MAXSIZE * AGraphic.Height/ AGraphic.Width)+pnBottom.Height;
  end;
end;

class function TformImagePrintPreview.PrintNow(AGraphic: TGraphic; ANotes : string = ''): boolean;
var
  form : TformImagePrintPreview;
begin
  form := TformImagePrintPreview.Create(nil);
  try
    form.Init(AGraphic,ANotes);
    result := form.ShowModal = mrOk;
  finally
    FreeAndNil(form);
  end;
end;

end.
Sign up to request clarification or add additional context in comments.

2 Comments

hi wath is Tdrawproc operation??
for example: image.cavas.linto(100,100);// printer.beginDoc;// printer.cavas.streachdraw(Rect(0,0,200,200),image.picture.Graphic);// printer.endDoc;// but printer create empty page
1

Perhaps you ignore the fact that the printing page has different resolution from a screen page.
The points you want to draw on the printing canvas should be multiplied by a factor, like Zartog uses in his example:

  PixPerInchX := GetDeviceCaps(bmp.Canvas.Handle, LOGPIXELSX);
  PixPerInchY := GetDeviceCaps(bmp.Canvas.Handle, LOGPIXELSY);
  bmp.Width := PixPerInchX * AWidth;
  bmp.Height := PixPerInchY * AHeight;

Here is a simple tutorial on how to print: Printing Directly from Delphi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.