Solved

Printing bitmaps

Posted on 2003-11-17
6
344 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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