?
Solved

Printing in Delphi..

Posted on 2002-07-08
2
Medium Priority
?
282 Views
Last Modified: 2010-04-04
Does anybody know or has an example on how to print something graphical in delphi? Please help me! TNX
0
Comment
Question by:ssdjgru1
2 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7137274
you've a canvas on the printer-object,
there you can draw your graphic as you need

meikl ;-)
0
 
LVL 7

Accepted Solution

by:
Cynna earned 800 total points
ID: 7137299
ssdjgru1,

If you have something drawn on a TPicture or TBitmap you can print it using following code (based on code posted on http://www.efg2.com/Lab/Library/Delphi/Printing/index.html):

COPY/PASTE:
----------------

procedure PrintBitmap(Bitmap:  TBitmap;
                      TopOfPagePercent, LeftMarginPercent, PrintedImageWidthPercent: Integer);
var
    BitmapHeader      :  pBitmapInfo;
    BitmapImage       :  POINTER;
    HeaderSize        :  DWORD;
    ImageSize         :  DWORD;
    FromLeftMargin    :  INTEGER;
    PrintedImageWidth :  INTEGER;
    FromTopOfPage     :  INTEGER;
    PrintedImageHeight:  INTEGER;

begin
 try
    Printer.BeginDoc; // Uses printers
    FromTopOfPage      := MulDiv(Printer.PageHeight, TopOfPagePercent  ,100);
    FromLeftMargin     := MulDiv(Printer.PageWidth , LeftMarginPercent ,100);
    PrintedImageWidth  := MulDiv(Printer.PageWidth , PrintedImageWidthPercent ,100);
    // Maintain aspect ratio
    PrintedImageHeight := Bitmap.Height*PrintedImageWidth div Bitmap.Width;
    GetDIBSizes(Bitmap.Handle, HeaderSize, ImageSize);
    GetMem(BitmapHeader, HeaderSize);
    GetMem(BitmapImage,  ImageSize);
    GetDIB(Bitmap.Handle, Bitmap.Palette, BitmapHeader^, BitmapImage^);
    StretchDIBits(Printer.Canvas.Handle,
                  FromLeftMargin, FromTopOfPage,          // Destination Origin
                  PrintedImageWidth, PrintedImageHeight,  // Destination Width & Height
                  0, 0,                                   // Source Origin
                  Bitmap.Width, Bitmap.Height,            // Source Width & Height
                  BitmapImage,
                  TBitmapInfo(BitmapHeader^),
                  DIB_RGB_COLORS,
                  SRCCOPY)
 finally
    FreeMem(BitmapHeader);
    FreeMem(BitmapImage);
    Printer.EndDoc;
 end;
end;




DEMO:
--------

(place Image1, Button1 on the form)

procedure TForm1.Button1Click(Sender: TObject);
begin
  Image1.Picture.LoadFromFile('C:\Program Files\Common Files\Borland Shared\Images\Splash\256Color\handshak.bmp');
  PrintBitmap(Image1.Picture.Bitmap, 1, 1, 80);
end;
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 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