Solved

Printing bitmaps

Posted on 2003-11-17
6
338 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

21 Experts available now in Live!

Get 1:1 Help Now