form上有几个image,image里都放着图片,把这几张图片按照它在form上的排列和大小打印出来,我用printer.Canvas.StretchDraw打印出的图象很小,看资料说要用GetDeviceCaps 取得打印机的分辨率,我想问一下,我看了函数GetDeviceCaps的帮助,不是太明白,他的返回值是不是每英寸的像素点值。如果是,比如说image在form中的位置为(left:=80,top:=100,width:=200,height:=300),那么下面的代码中r1应该为什么呢?也就是说取得打印机的分辨率后怎么转化为r1所需要的值?另外上面的80,100,200,300它的单位是多少?
var
r1,:TRect;
x,y:integer;
begin
x:=GetDeviceCaps(printer.Handle, logPixelsX);
y:=GetDeviceCaps(printer.Handle, logPixelsY);
r1:=Rect(?)
printer.BeginDoc;
printer.Canvas.StretchDraw(r1,image.Picture.Graphic);
printer.EndDoc;
end;

解决方案 »

  1.   

    var
      Form1: TForm1;
      ppix, ppiy: Integer;implementation
    {$R *.dfm}procedure Getppi(DC: HDC);
    begin
      ppix:=GetDeviceCaps(DC,logPixelsX);
      ppiy:=GetDeviceCaps(DC,logPixelsY);
    end;
    function CmToPixelX(cm: Real): Integer;
    begin
       Result := Trunc(Cm * ppix / 2.54);
    end;function CmToPixelY(cm: Real): integer;
    begin
      Result := Trunc(Cm * ppiy /2.54);
    end;//前面定义一个过程和两个函数,那样就不会因打印机的分辩率而失效:)
    procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
       getppi(printer.handle);
       x:=CmToPixelX(2.5);     //距水平页边距 2.5cm
       y:=cmtopixely(2.7);    //距垂直页边距 2.7cm
       printer.BeginDoc;
       printer.canvas.draw(x,y,image1.picture.graphic);
       Printer.endDoc;
    end;
      

  2.   

    2、利用打印机画布canvas的stretchdraw方法可以对图形进行更为灵活的处理。其中要用到一个代表图形输出区域的Rect参数Trect的类型定义如下:TRect = recordcase Integer of0: (LeftTopRightBottom: Integer);1: (TopLeftBottomRight: TPoint);end;  
    例:通过调整Rect的范围、图形大小及其在打印页面上的位置,实现图形打印:procedure TForm1.Button1Click(Sender: TObject);var strect:Trect; //定义打印输出矩形框的大小temhitemwd:integer;beginif printdialog1.execute thenbegintemhi:=image1.picture.height;temwd:=image1.picture.width;while (temhi<printer.pageheight div 2) and //将图形放大到打印页面的1/2 (temwd<printer.pagewidth div 2) do begintemhi:=temhi+temhi;temwd:=temwd+temwd;end;with strect do //定义图形在页面上的中心位置输出beginleft:=(printer.pagewidth -temwd) div 2;top:=(printer.pageheight-temhi) div 2;right:=left+temwd;bottom:=top+temhi;end;with Printer do beginbegindoc; //将放大的图形向打印机输出canvas.stretchdraw(strectimage1.picture.graphic);enddoc;end;end;end; 
      

  3.   

    打印form的例子(包括Form上的所有东西,可能不是你所需要的,但可以参考),实现的很不错:uses Printers;type
      PPalEntriesArray = ^TPalEntriesArray; {for palette re-construction}
      TPalEntriesArray = array[0..0] of TPaletteEntry;procedure BltTBitmapAsDib(DestDc : hdc;   {Handle of where to blt}
                              x : word;       {Bit at x}
                              y : word;       {Blt at y}
                              Width : word;   {Width to stretch}
                              Height : word;  {Height to stretch}
                              bm : TBitmap);  {the TBitmap to Blt}
    var
      OriginalWidth :LongInt;               {width of BM}
      dc : hdc;                             {screen dc}
      IsPaletteDevice : bool;               {if the device uses palettes}
      IsDestPaletteDevice : bool;           {if the device uses palettes}
      BitmapInfoSize : integer;             {sizeof the bitmapinfoheader}
      lpBitmapInfo : PBitmapInfo;           {the bitmap info header}
      hBm : hBitmap;                        {handle to the bitmap}
      hPal : hPalette;                      {handle to the palette}
      OldPal : hPalette;                    {temp palette}
      hBits : THandle;                      {handle to the DIB bits}
      pBits : pointer;                      {pointer to the DIB bits}
      lPPalEntriesArray : PPalEntriesArray; {palette entry array}
      NumPalEntries : integer;              {number of palette entries}
      i : integer;                          {looping variable}
    begin
    {If range checking is on - lets turn it off for now}
    {we will remember if range checking was on by defining}
    {a define called CKRANGE if range checking is on.}
    {We do this to access array members past the arrays}
    {defined index range without causing a range check}
    {error at runtime. To satisfy the compiler, we must}
    {also access the indexes with a variable. ie: if we}
    {have an array defined as a: array[0..0] of byte,}
    {and an integer i, we can now access a[3] by setting}
    {i := 3; and then accessing a[i] without error}
    {$IFOPT R+}
      {$DEFINE CKRANGE}
      {$R-}
    {$ENDIF} {Save the original width of the bitmap}
      OriginalWidth := bm.Width; {Get the screen's dc to use since memory dc's are not reliable}
      dc := GetDc(0);
     {Are we a palette device?}
      IsPaletteDevice :=
        GetDeviceCaps(dc, RASTERCAPS) and RC_PALETTE = RC_PALETTE;
     {Give back the screen dc}
      dc := ReleaseDc(0, dc); {Allocate the BitmapInfo structure}
      if IsPaletteDevice then
        BitmapInfoSize := sizeof(TBitmapInfo) + (sizeof(TRGBQUAD) * 255)
      else
        BitmapInfoSize := sizeof(TBitmapInfo);
      GetMem(lpBitmapInfo, BitmapInfoSize); {Zero out the BitmapInfo structure}
      FillChar(lpBitmapInfo^, BitmapInfoSize, #0); {Fill in the BitmapInfo structure}
      lpBitmapInfo^.bmiHeader.biSize := sizeof(TBitmapInfoHeader);
      lpBitmapInfo^.bmiHeader.biWidth := OriginalWidth;
      lpBitmapInfo^.bmiHeader.biHeight := bm.Height;
      lpBitmapInfo^.bmiHeader.biPlanes := 1;
      if IsPaletteDevice then
        lpBitmapInfo^.bmiHeader.biBitCount := 8
      else
        lpBitmapInfo^.bmiHeader.biBitCount := 24;
      lpBitmapInfo^.bmiHeader.biCompression := BI_RGB;
      lpBitmapInfo^.bmiHeader.biSizeImage :=
        ((lpBitmapInfo^.bmiHeader.biWidth *
          longint(lpBitmapInfo^.bmiHeader.biBitCount)) div 8) *
          lpBitmapInfo^.bmiHeader.biHeight;
      lpBitmapInfo^.bmiHeader.biXPelsPerMeter := 0;
      lpBitmapInfo^.bmiHeader.biYPelsPerMeter := 0;
      if IsPaletteDevice then begin
        lpBitmapInfo^.bmiHeader.biClrUsed := 256;
        lpBitmapInfo^.bmiHeader.biClrImportant := 256;
      end else begin
        lpBitmapInfo^.bmiHeader.biClrUsed := 0;
        lpBitmapInfo^.bmiHeader.biClrImportant := 0;
      end; {Take ownership of the bitmap handle and palette}
      hBm := bm.ReleaseHandle;
      hPal := bm.ReleasePalette; {Get the screen's dc to use since memory dc's are not reliable}
      dc := GetDc(0);  if IsPaletteDevice then begin
       {If we are using a palette, it must be}
       {selected into the dc during the conversion}
        OldPal := SelectPalette(dc, hPal, TRUE);
       {Realize the palette}
        RealizePalette(dc);
      end;
     {Tell GetDiBits to fill in the rest of the bitmap info structure}
      GetDiBits(dc,
                hBm,
                0,
                lpBitmapInfo^.bmiHeader.biHeight,
                nil,
                TBitmapInfo(lpBitmapInfo^),
                DIB_RGB_COLORS); {Allocate memory for the Bits}
      hBits := GlobalAlloc(GMEM_MOVEABLE,
                           lpBitmapInfo^.bmiHeader.biSizeImage);
      pBits := GlobalLock(hBits);
     {Get the bits}
      GetDiBits(dc,
                hBm,
                0,
                lpBitmapInfo^.bmiHeader.biHeight,
                pBits,
                TBitmapInfo(lpBitmapInfo^),
                DIB_RGB_COLORS);
      if IsPaletteDevice then begin
       {Lets fix up the color table for buggy video drivers}
        GetMem(lPPalEntriesArray, sizeof(TPaletteEntry) * 256);
       {$IFDEF VER100}
          NumPalEntries := GetPaletteEntries(hPal,
                                             0,
                                             256,
                                             lPPalEntriesArray^);
       {$ELSE}
          NumPalEntries := GetSystemPaletteEntries(dc,
                                                   0,
                                                   256,
                                                   lPPalEntriesArray^);
       {$ENDIF}
        for i := 0 to (NumPalEntries - 1) do begin
          lpBitmapInfo^.bmiColors[i].rgbRed :=
            lPPalEntriesArray^[i].peRed;
          lpBitmapInfo^.bmiColors[i].rgbGreen :=
            lPPalEntriesArray^[i].peGreen;
          lpBitmapInfo^.bmiColors[i].rgbBlue :=
            lPPalEntriesArray^[i].peBlue;
        end;
        FreeMem(lPPalEntriesArray, sizeof(TPaletteEntry) * 256);
      end;  if IsPaletteDevice then begin
       {Select the old palette back in}
        SelectPalette(dc, OldPal, TRUE);
       {Realize the old palette}
        RealizePalette(dc);
      end; {Give back the screen dc}
      dc := ReleaseDc(0, dc); {Is the Dest dc a palette device?}
      IsDestPaletteDevice :=
        GetDeviceCaps(DestDc, RASTERCAPS) and RC_PALETTE = RC_PALETTE;
      if IsPaletteDevice then begin
       {If we are using a palette, it must be}
       {selected into the dc during the conversion}
        OldPal := SelectPalette(DestDc, hPal, TRUE);
       {Realize the palette}
        RealizePalette(DestDc);
      end; {Do the blt}
      StretchDiBits(DestDc,
                    x,
                    y,
                    Width,
                    Height,
                    0,
                    0,
                    OriginalWidth,
                    lpBitmapInfo^.bmiHeader.biHeight,
                    pBits,
                    lpBitmapInfo^,
                    DIB_RGB_COLORS,
                    SrcCopy);  if IsDestPaletteDevice then begin
       {Select the old palette back in}
        SelectPalette(DestDc, OldPal, TRUE);
       {Realize the old palette}
        RealizePalette(DestDc);
      end; {De-Allocate the Dib Bits}
      GlobalUnLock(hBits);
      GlobalFree(hBits); {De-Allocate the BitmapInfo}
      FreeMem(lpBitmapInfo, BitmapInfoSize); {Set the ownership of the bimap handles back to the bitmap}
      bm.Handle := hBm;
      bm.Palette := hPal;  {Turn range checking back on if it was on when we started}
    {$IFDEF CKRANGE}
      {$UNDEF CKRANGE}
      {$R+}
    {$ENDIF}
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if PrintDialog1.Execute then begin
        Printer.BeginDoc;
        BltTBitmapAsDib(Printer.Canvas.Handle,
                        0,
                        0,
                        Image1.Picture.Bitmap.Width,
                        Image1.Picture.Bitmap.Height,
                        Image1.Picture.Bitmap);
        Printer.EndDoc;
      end;
    end;
      

  4.   

    to tommy_linux(津工之鸟):
      你好,谢谢你的代码,你的第一个例子中[距水平页边距 2.5cm]我想知道指的是距打印纸页边距 2.5cm吧,我需要的是根据image在form上的位置打印,这个时候x:=CmToPixelX(2.5)中括号内应为何值呢?这也是我问80,100,200,300它的单位是多少的原因?另外我试过Trunc(Cm * ppix / 2.54),就是不知道cm应该代入的值为多少。用上面的left:=80,top:=100打出来图更小。
      

  5.   

    print.canvas.textout(100,500 ,edit.text);就表示在打印纸从左边算起100个点,
    顶部算起500个点的地方打印edit1.text内容。这里的点含义就是打印机分辨率DPI
    中的每英寸多少点的这个点。比如600DPI的分辨率就表示一英寸有600个点,那么上
    面那个打印命令就是在坐标为(1/6英寸,5/6英寸)的地方打印了edit1.text的内容。
      

  6.   

    to tommy_linux(津工之鸟):
      比如说image在form中的位置是left100,top500,那么打在打印纸上应该不是从左边算起100个点,顶部算起500个点的地方吧?它们之间应该如何转换呢?谢谢。
      

  7.   


    To:ghbh(我想编但是编不出来)你好! 我看了你的短信。
    我在另外那个近似的帖子的回复中,所说的那种方法,简单
    但不是很精确。但是,要以什么样的方式输出图片到打印机中,
    其实取决于你“想要做什么和你的要求”。我个人强烈向你推荐很好的两部书:
    出版社:人民邮电出版社
    作者:Steve Teixeira Xavier Pacheco
          (是写Delphi的作者之一,很牛哦,我们能有饭吃是靠他们哦)
    书名:Delphi 4 开发大全(上、下两部)(我不知英文名)
    哪里有卖,我就不知了,深圳这边大大小小的书店都没有,
    估计象广州、北京、上海等地才有。说这部书绝对经典,
    已经不是我一个说过了,真的值得拥有。大家努力!
      

  8.   

    to  LKJ99(阳江in深圳) :
      你好! 你是广东阳江的吧,其实我的要求不复杂:form上有几张图片,这几张图片紧挨着,把form上的几张图片按照它在form上的位置和大小打印出来,打在纸上哪个地方无所谓,只要图片还是基本紧挨着并且大小差不多就行。请问我应该用什么办法呢?