【问题标题】:(delphi)print image problem(delphi)打印图像问题
【发布时间】:2009-09-28 06:23:34
【问题描述】:

我会使用 lineto 函数在图像画布中绘图,然后将其打印出来。但是打印机会创建一个空页面。我使用了printer.canvas.StretchDraw 但是如果图像被保存然后加载,图像打印就会成功。有人知道原因吗?

【问题讨论】:

  • 你能展示一下所涉及的代码吗?如果我们看到您在做什么,就更容易提供帮助。

标签: delphi


【解决方案1】:

这是我使用的:

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;

DrawProc 在哪里:

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

而图片预览是:

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.

【讨论】:

  • 你好,是 Tdrawproc 操作吗??
  • 例如:image.cavas.linto(100,100);//printer.beginDoc;//printer.cavas.streachdraw(Rect(0,0,200,200),image.picture.Graphic);// printer.endDoc;// 但打印机创建空白页
【解决方案2】:

也许您忽略了打印页面与屏幕页面的分辨率不同这一事实。
您要在打印画布上绘制的点应乘以一个因子,例如 Zartog 在他的示例中使用:

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

这里有一个简单的打印教程:Printing Directly from Delphi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多