Scalabium Software

SMExport advertising
Knowledge for your independence'.
Home Delphi and C++Builder tips


#90: How can I print a bitmap on printer?

Sometimes you must from own application to print a some bitmap.

Of course, you can use the some report designer and create a report form with this bitmap. But you can solve this task and easy - to print a bitmap on printer canvas directly without any report engine.

Someone from you can say - easy task with solution in few lines of code:

with Printer do
begin
  BeginDoc;
  Canvas.Draw(X, Y, bmp);
  EndDoc;
end;

But this code will work correctly not with any printer! For example, Epson Stylus 300 this code will print the image with very bad quality and on some printer you can receive a blank paper... The reason of this - using the StretchBlt function for output but in MSDN you can read that this function does not guarantee a correct outputing from one device to other.

As alternative you must use the StretchDIBits function:

var
  Info: PBitmapInfo;
  InfoSize: DWORD;
  Image: Pointer;
  ImageSize: DWORD;
  Bits: HBITMAP;
  DIBWidth, DIBHeight: LongInt;
begin
  Printer.BeginDoc;
  try
    Canvas.Lock;
    try
      { Paint bitmap to the printer }
      with Printer do
      begin
        Bits := bmp.Handle;
        GetDIBSizes(Bits, InfoSize, ImageSize);
        Info := AllocMem(InfoSize);
        try
          Image := AllocMem(ImageSize);
          try
            GetDIB(Bits, 0, Info^, Image^);
            with Info^.bmiHeader do
            begin
              DIBWidth := biWidth;
              DIBHeight := biHeight;
            end;
            StretchDIBits(Canvas.Handle, 0, 0, DIBWidth, DIBHeight, 0, 0,
              DIBWidth, DIBHeight, Image, Info^, DIB_RGB_COLORS, SRCCOPY);
          finally
            FreeMem(Image, ImageSize);
          end;
        finally
          FreeMem(Info, InfoSize);
        end;
      end;
    finally
      Canvas.Unlock;
    end;
  finally
    Printer.EndDoc;
  end;
end;


Published: July 12, 2000

See also
 
ABA Document Convert
Clarion to Text converter
Paradox ActiveX
Excel Web-stream
DBISAM Viewer
ABA Picture Convert
Protected Storage Viewer
ABA Database Convert
DBISAM Password Recovery
Clarion Viewer
 
 


Contact to webmaster

 

Borland Software Code Gear Scalabium Delphi tips

Copyright© 1998-2024, Scalabium Software. All rights reserved.
webmaster@scalabium.com

SMExport/SMImport suites