Home arrow Visual Basic arrow Page 4 - Printing With Visual Basic

Printing With Visual Basic

Despite claims that computers would create a "paperless society", printing is still one of the most important functions of any application. Visual Basic makes it easy to print, and in this article James shows you what you need to know to print text, shapes and images with Visual Basic. If you need to create your own invoicing or reporting system with Visual Basic then this article is a definite must read.

Author Info:
By: James Crowley
Rating: 4 stars4 stars4 stars4 stars4 stars / 306
April 07, 2002
  1. · Printing With Visual Basic
  2. · Printing with VB
  3. · Printing graphics
  4. · More Printing in VB
  5. · Conclusion

print this article

Printing With Visual Basic - More Printing in VB
(Page 4 of 5 )

One other function is the PaintPicture method that allows you to output the contents of a graphics file to the printer object. The PaintPicture method uses this syntax:

Printer.PaintPicture picture, x1, y1, [width1], [height1], [x2], [y2], [width2], [height2], [opcode]

I won't cover everything here, so if you want more info, open up the MSDN library and select 'PaintPicture method' from the Index list. Picture is a IPictureDisp class, which can be retrieved from a PictureBox or Forms Picture property, or using LoadPicture.

x1 and y1 specify the coordinates where the picture should be placed on the page. Width1 and Height1 set the width and height of the picture to be outputted, respectively. x2 and y2, in combination with width2 and height2 can be used to specify a clipping area from the source image.

Take a look at the examples below.

The code below outputs the picture contained within Picture1 to the printer with 1000 twip (if this is the current scale mode) margins.

Printer.PaintPicture Picture1.Picture, 1000, 1000

This code,

PaintPicture LoadPicture("C:sample.jpg"), 1000, 1000

loads the file C:sample.jpg and outputs it onto a page. Finally,

Printer.PaintPicture Picture1.Picture, 1000, 1000, 2000, 2000

resizes the picture in Picture1 to 2000x2000 pixels, and prints it.

Both the Circle and the Line command allow you to specify the line colour, but you appear to have no control over the line width, what units the distances are measured in or any fill colours.

As all of these are fairly simple, they are explained briefly below:
  • FillColor: This specifies the fill color (in RGB), if any. Use the RGB function or VB colour constants to generate values for this parameter.
  • FillMode: This sets the pattern used to fill any shapes drawn by the Printer object. To see its possible values, hit F2, and select FillStyleConstants from the Classes list.
  • DrawMode: The mode of drawing. Unless you know what you are doing, leave this to 13 - Copy Pen. Many of these settings yield unpredictable results depending on the colours on the screen. See DrawModeConstants in the object browser for its values.
  • DrawWidth: The width of the drawing line.
  • DrawStyle: The style of the line. (ie solid, dotted etc). See DrawStyleConstants in the object browser for its values.
  • ScaleMode: This specifies the units in which coordinates are measured. The default is twips (1,440 twips equal one inch).
Aside from graphics, there are also a number of properties that directly affect printing. I'll cover these here:
  • Copies: The number of copies of your output that will be printed.
  • DeviceName: The name of the printer device.
  • DriverName: The name of the driver for the printer.
  • Duplex: Whether the printer is in Duplex mode or not (printing on both sides).
  • hDC: A handle to the printers device context.
  • Orientation: The orientation of the paper. This can be vbPRORLandscape or vbPRORPortrait.
  • Page: Returns the current page number (read-only).
  • PaperBin: The paper bin to use when printing the document. The values for this property can be found by hitting F2 (Object Browser), selecting PrinterObjectConstants from the Classes list, and taking a look at all the constants beginning with vbPRBN.
  • PaperSize: The size of the paper that is being printed on. See PrinterObjectConstants in the Object Browser, beginning with vbPRPS.
  • Port: The name of the printer port (read-only).
  • PrintQuality: The quality of printing (printer resolution). See PrinterObjectConstants, beginning with vbPRPQ.
  • TrackDefault: Whether to automatically move to the default printer if this is changed in Control Panel.
  • Zoom: The percentage with which the output is scaled up or down. Note that this only works on printers that support it!.

blog comments powered by Disqus

- Developing an XML Web Service Using Visual S...
- Creating an HTML File List with VB
- Fun with Email: VB6, CDO, MAPI, and a Remote...
- Extranet/Intranet Dictionary Cracker in VB
- Finding Default App Icons With Visual Basic
- Registry Fever With Visual Basic
- Implementing An ADO Data Control With VB6
- Printing With Visual Basic
- MSMQ Part 1/2: Architecture and Simple Imple...
- Magnifying The Desktop With Visual Basic
- Sending Email With MAPI Components in Visual...
- Two Person Chat With The Winsock Control And...
- A Real-Time ActiveX News Control
- Accessing the Windows API in Visual Basic

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials