Just received some email from a VB 6.0 enthusiast asking how to print graphics in Visual Basic 2005.

He said it was much easier in VB 6.0 and I am embarrassed to say I don’t even remember how we used to print images from Visual Basic 6.0 so I cannot even agree or disagree with that statement!  In either case to do this in Visual Basic 2005 it is two lines of code - one line to draw the image to the printdocument control and the second line to print it...

 

1.       Create a new Windows Application

2.       Drag a print document control to the Form

3.       Create a printpage event handler for the printdocument control.  (In code view select the printdocument1 object in the objects drop down and under events  dropdown select printpage and an event handler will be created for you)

4.       Use the graphics object passed to the printpage event to invoke the Drawimage method

 

Private Sub PrintDocument1_PrintPage(....

   PrintDocument1.PrintPage

   e.Graphics.DrawImage(New Bitmap("c:\yellow.jpg"), 1, 1)

End Sub

5.       To print you just call the print() method on the printdocument control a button click or whatever 

 

    Private Sub Button2_Click(

               PrintDocument1.Print()

 End Sub  

 

 

6.       To really add some sizzle add a printpreview dialog control to your form and give your app print preview functionality with the code below….(it will display the graphics from your image in step #4)

Private Sub Button1_Click(

PrintPreviewDialog1.Document = PrintDocument1

   PrintPreviewDialog1.Show()

End Sub