Solved

Printing in Delphi..

Posted on 2002-07-08
2
259 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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

11 Experts available now in Live!

Get 1:1 Help Now