Strona g│≤wna

 

Drukowanie bitmapy

CzΩsto mo┐na mieµ problemy z prawid│owym wydrukowaniem bitmapy. 
Oto kod, kt≤ry nades│a│ czytelnik. DziΩki temu stworzona zostanie bitmapa do kt≤rej zaimportowana zostaje zawarto╢µ komponentu "Panel". NastΩpnie drukowana zostaje sama bitmapa. 

Niestety autor nie doda│ do kodu komentarzy. 

Listing 3.1


unit junit;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Button1: TButton;
    CheckBox1: TCheckBox;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure PrintBitmap(Canvas:  TCanvas; DestRect:  TRect;  Bitmap: TBitmap);
var
  BitmapHeader: pBitmapInfo;
  BitmapImage: pointer;
  HeaderSize: DWord;
  ImageSize: DWord;
BEGIN
  GetDIBSizes(Bitmap.Handle, HeaderSize, ImageSize);
  GetMem(BitmapHeader, HeaderSize);
  GetMem(BitmapImage,  ImageSize);
  try
  GetDIB(Bitmap.Handle, Bitmap.Palette, BitmapHeader^, BitmapImage^);
  StretchDIBits(Canvas.Handle,
                DestRect.Left, DestRect.Top,
                DestRect.Right  - DestRect.Left,
                DestRect.Bottom - DestRect.Top,
                0, 0,
                Bitmap.Width, Bitmap.Height,
                BitmapImage,TBitmapInfo(BitmapHeader^),
                DIB_RGB_COLORS,SRCCOPY)
  finally
    FreeMem(BitmapHeader);
    FreeMem(BitmapImage)
  end;
end;


procedure TForm1.Button2Click(Sender: TObject);
var
   im : TBitMap;
   printarea : TRect;
begin
     Printer.BeginDoc;
        try
        im := TBitMap.Create;
        try
           im.PixelFormat := pf24bit; //<<===

           im.Width := panel1.Width;
           im.Height := panel1.Height;

           im.Canvas.CopyRect(Rect(0,0, im.Width,im.Height),
                                form1.Canvas,
                                Rect(Panel1.Left, Panel1.Top,
                                     Panel1.Left + Panel1.Width-1,
                                     Panel1.Top  +
Panel1.Height-1));
           printarea.Left := MulDiv(Printer.PageWidth, 10,100);
           printarea.Right := MulDiv(Printer.PageWidth, 80,100);
           printarea.Top := MulDiv(Printer.PageHeight,10,100);
           printarea.Bottom := MulDiv(printarea.Right, im.Height,
im.Width);
           PrintBitmap(Printer.Canvas, printarea, im);
        finally
           im.free;
        end;
     finally
        Printer.EndDoc;
     end;

end;



end.

Przydatne linki:

  • podstawy drukowania; [ Zobacz ]

Gotowy program mo┐esz ╢ci▒gn▒µ tutaj:

stretchdibits.zip ( 4 kB )

Autor: ???