Printing a Bitmap & memobox

the_smudge
the_smudge used Ask the Experts™
on
Hello,

I've been working on a program for making football plays.  I've been able to make a subprogram that will take the text out of the memobox and print it, but I have no clue on how to print a pre-saved bitmap that I don't want to open on the screen to print.  It will always be called "C:\football porgram\print.bmp".  If anyone could help me on how to do this, it would be much appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Are you using TPrinter for printing? If so you can simply load the bitmap into a TBitmap and use
TPrinter.Canvas.Draw()

Author

Commented:
Well, I really have no idea how to use the print functions...I don't know how to use the print dialog...never got into it.  But, here's the code I have for printing the memobox, which maybe will give you some insight as to what I'm using for printing:

procedure PrintStrings(Strings: TStrings);
var
  Prnt: TextFile;
  i: word;
begin
  AssignPrn(Prnt);
  try
    Rewrite(Prnt);
    try
      for i := 0 to Strings.Count - 1 do
        writeln(Prnt, Strings.Strings[i]);
    finally
      CloseFile(Prnt);
    end;
  except
    on EInOutError do
      MessageDlg('Error Printing text.', mtError, [mbOk], 0);
  end;
end;

I just want to be able to add in there above the memobox the bitmap...and, if possible to center the stuff on the page...but that isn't absolutely necessary.  Thanks for the reply.

Commented:
Okay, that method is fine if you just want to print text. To do any kind of formatting you really ought to use the TPrinter class Delphi has built in. It's really quite simple to use. Just include 'Printer' in your uses clause and then you can do something like below:

*code taken verbatim from the Delphi Help files*

procedure TForm1.Button1Click(Sender: TObject);
var
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  try
    Bmp.LoadFromFile('MyBitmap.bmp');
    with Printer do
    begin
      BeginDoc;
      Canvas.Draw((PageWidth - Bmp.Width) div 2,
                  (PageHeight - Bmp.Height) div 2,
                  Bmp);
      EndDoc;
    end;
  finally
    Bmp.Free;
  end;

end;

This will print your bitmap in the center of the page. You can use Canvas.DrawText to write out your text.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
Thanks Smande00...that seemed to almost work...but when I print it'll just print like one teeny bit of the picture and it'll be a lot smaller too.  Any idea how to change it?  Like, I'll open it in paint and it's a heckuva lot bigger, but with that command it doesn't even print the whole thing.  And, since, as I said earlier, I know nothing about the print function, I'm kinda lost.  thanks for the help !
- Smudge -
Hi,

Try this:

procedure TForm1.Button1Click(Sender: TObject);
var
  ScaleX, ScaleY: Integer;
  R: TRect;
  Bmp: TBitmap;
begin
 Bmp := TBitmap.Create;
 try
  Bmp.LoadFromFile('C:\football porgram\print.bmp');
  Printer.BeginDoc;
  with Printer do
  try
    ScaleX := GetDeviceCaps(Handle, logPixelsX) div PixelsPerInch;
    ScaleY := GetDeviceCaps(Handle, logPixelsY) div PixelsPerInch;
    R := Rect(0, 0, Bmp.Width * ScaleX,
      Bmp.Height * ScaleY);
    Canvas.StretchDraw(R, Bmp);
  finally
    EndDoc;
  end;
 finally
   Bmp.Free;
 end;
end;

Regards, Geo

Commented:
Yes that's exactly it Geobul. Now your graphic will print out in the exact same size as it appears on your screen. So if you want to resize it from here just modify

 R := Rect(0, 0, Bmp.Width * ScaleX,
                 Bmp.Height * ScaleY);

to:
 
 R := Rect(0, 0, Bmp.Width * ScaleX *PercentSizeX,
     Bmp.Height * ScaleY* PercentSizeY);

and that should do it.

Author

Commented:
Thanks for the help so far guys...the code looks like it should work, but I get the following error:
[Error] footballS.pas(190): Undeclared identifier: 'PixelsPerInch'

I'm assuming I'm just missing something I need in the uses clause, but I don't know what.  Thanks again

- Smudge -

Author

Commented:
(here's my uses list)

uses
  Windows, Messages, SysUtils, Classes, Printers,  Graphics, Controls, Forms, Dialogs,
  Menus, ActnList, StdCtrls, ExtCtrls;
replace with Screen.PixelsPerInch

Sorry. I used the code from a Form.

Author

Commented:
Thanks for the help with the bitmap.  I'm still not quite sure how to use the printer command to print the memobox, but I'll work on that some I guess to figure it out.
- Smudge -

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial