Print Bitmap as DIB
by Boris Nienke
(you need a Form with a TImage and a TButton)
– just for 24bit bitmaps. If bitmaps has less bits, then there may be a blank page on some HP printers! You can change color-depth with: “MyBitmap.PixelFormat := pf24bit”.
– or you need to fill the palette-entries (look at “A Better Way to Print a Form” (Borland Technical Info TI1412D.txt)
[code lang=”delphi”]procedure MemFree(Ptr: Pointer; Size: Longint);
begin
  if Size < 65535 then
    FreeMem(Ptr, Size)
  else
    GlobalFreePtr(Ptr);
end;
  {Prints BitMap }
procedure DrawImage(Canvas: TCanvas; DestRect: TRect; ABitmap: TBitmap);
var
  Header, Bits: Pointer;
  HeaderSize: Integer;
  {$IfDef Win32}                   {Checks if using Delphi 2.0}
  BitsSize : Integer;
  {$Else}
  BitsSize : LongInt;
  {$EndIf}
begin
  GetDIBSizes(ABitmap.Handle, HeaderSize, BitsSize);
  Header := AllocMem(HeaderSize);
  Bits := AllocMem(BitsSize);
  try
    GetDIB(ABitmap.Handle, ABitmap.Palette, Header^, Bits^);
    StretchDIBits(Canvas.Handle, DestRect.Left, DestRect.Top,
        DestRect.Right - DestRect.Left, DestRect.Bottom - DestRect.Top,
        0, 0, ABitmap.Width, ABitmap.Height, Bits, TBitmapInfo(Header^),
        DIB_RGB_COLORS, SRCCOPY);
  finally
    MemFree(Header, HeaderSize);
    MemFree(Bits, BitsSize);
  end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  Printer.BeginDoc;
  DrawImage(Printer.Canvas,
            Rect(20, 30, 20+(image1.picture.bitmap.width * 7), 30+(image1.picture.bitmap.height * 7)),
            image1.picture.bitmap);
  Printer.EndDoc;
end;
[/code]
[tags]Delphi, Printing, Graphic[/tags]
0 Kommentare zu “Print Bitmap as DIB”