Solved

Printing bitmaps

Posted on 2003-11-17
6
341 Views
Last Modified: 2010-08-05
Any ideas why I get a blank sheet of paper from this? Irrelevant parts of code removed for clarity.

PROCEDURE PrintBitmap(Canvas:  TCanvas; DestRect:  TRect;  Bitmap:  TBitmap);
  VAR
    BitmapHeader:  pBitmapInfo;
    BitmapImage :  POINTER;
    HeaderSize  :  DWORD;    // Use DWORD for D3-D5 compatibility
    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,     // Destination Origin
                                                      DestRect.Right  - DestRect.Left, // Destination Width
                  DestRect.Bottom - DestRect.Top,  // Destination Height
                  0, 0,                            // Source Origin
                  Bitmap.Width, Bitmap.Height,     // Source Width & Height
                  BitmapImage,
                  TBitmapInfo(BitmapHeader^),
                  DIB_RGB_COLORS,
                  SRCCOPY)
  FINALLY
    FreeMem(BitmapHeader);
    FreeMem(BitmapImage)
  END
END {PrintBitmap};

procedure TDebugForm.Button1Click(Sender: TObject);
var
      i, x, y : integer;
      bmp : TBitmap;
      dr, sr : TRect;
      col : TColor;
      prt : TPrinter;
begin
      bmp := TBitmap.Create;
      bmp.Width := 128 * 9 + 1;
      bmp.Height := 48 * 17 + 1;
      prt := Printer;
      prt.Orientation := poLandscape;
      prt.BeginDoc;

      for i := 1 to 8 do begin
            bmp.Canvas.Brush.Color := clSilver;
            bmp.Canvas.Pen.Color := clAqua;
            bmp.Canvas.Rectangle(0, 0, bmp.Width, bmp.Height);
            // more drawing stuff here
            bmp.SaveToFile('vw' + IntToStr(i) + '.bmp');

            SetRect(dr, 0, 0, prt.PageWidth, prt.PageHeight);
            if i > 1 then prt.NewPage;
            PrintBitmap(prt.Canvas, dr, bmp);
      end;
      prt.EndDoc;
end;

The bitmap is known to contain data, since it saves it to disk and I can load it in PaintShopPro. Drawing manually on the printer's canvas works fine (prt.Canvas.TextOut()).

NT4/D7Pro/EpsonC4000

Geoff M.
0
Comment
Question by:gmayo
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:sgc_romania
ID: 9764923
try to use

PrintBitmap(CanvasHandle:  THandle; DestRect:  TRect;  Bitmap:  TBitmap);
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9764992
this may take no difference, sgc

just wondering about the complicated way
usualy i draw directly on the printer-canvas, with a bit ratio

but geoff will know why

looking later about the used api (guessing one parameter may not proper)

meikl ;-)
0
 
LVL 8

Author Comment

by:gmayo
ID: 9765477
SGC, if you look at the code, TCanvas is passed to the PrintBitmap routine, and the PrintBitmap routine uses Canvas.Handle in the call to StretchDIBits. So Meikl is right, it will make no difference.

Ordinary blits don't work (StretchDraw, CopyRect etc). Something to do with different devices (screen/printer I guess). It is documented (do a google for TPrinter and StretchDraw). The PrintBitmap routine was copied from one such site as a work-around to this problem. Why it doesn't work is beyond me.

Geoff M.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 9765512
well, too late now here,
must now have a bit family-life,
but will take tomorrow a look about this problem,
if not soved until then

see you

meikl ;-)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 250 total points
ID: 9770552
hi geoff,

tested your code -> works properly
seems to be a problem with your printer or device-driver

meikl ;-)
0
 
LVL 8

Author Comment

by:gmayo
ID: 9782538
It's beyond me this one. I guess I'll just have to do it manually. I'll give you the points on the basis that you did try it yourself.

Thanks

Geoff M.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now