Link to home
Start Free TrialLog in
Avatar of the_smudge
the_smudge

asked on

Printing a Bitmap & memobox

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.
Avatar of smande00
smande00

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

ASKER

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.
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.
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
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.
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 -
(here's my uses list)

uses
  Windows, Messages, SysUtils, Classes, Printers,  Graphics, Controls, Forms, Dialogs,
  Menus, ActnList, StdCtrls, ExtCtrls;
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 -