Solved

Printing in Delphi..

Posted on 2002-07-08
2
263 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 200 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
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, f…

863 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

25 Experts available now in Live!

Get 1:1 Help Now