tags:

views:

39

answers:

1

Does anyone have any code suggestions or samples for printing photos (BMP or TIFF or JPEG), using Visual Basic or .NET framework?

+2  A: 

VB6 and .NET handle printing quite differently. These examples are the bare minumum, just to give you an idea of the process.

In VB6 you control the printer step by step:

Private Sub PrintMyPicture()

  'Place to store my picture
  Dim myPicture As IPictureDisp
  'Load the picture into the variable
  Set myPicture = LoadPicture("C:\temp\myPictureFile.bmp")

  'Draw the picture on the printer, just off the edge of the page
  Printer.PaintPicture myPicture, 10, 10

  'Done printing!
  Printer.EndDoc
End Sub

Lo and behold, your picture will come out of the default printer. the PaintPicture method accepts width, height and a few other parameters to help get the image to fit, and the Printer object gives you all sorts of info about the printer.

In .Net it's kind of the other way around. You start printing, and the printer will raise an event for each page until you tell it to stop. Every page event gives you as graphics object upon which you can draw using all of the standard System.Drawing classes and methods:

'Class variable
Private WithEvents printer As System.Drawing.Printing.PrintDocument


' Kick off the printing process. This will cause the printer_PrintPage event chain to start firing.
Public Sub PrintMyPicture() 
'Set up the printer

    printer.PrinterSettings.PrinterName = "MyPrinterNameInPrintersAndFaxes"
    printer.Print()
End Sub

'This event will keep firing while e.HasMorePages = True.
Private Sub printer_PrintPage(ByVal sender As Object, ByVal e As  System.Drawing.Printing.PrintPageEventArgs) Handles printer.PrintPage



    'Load the picture
    Dim myPicture As System.Drawing.Image = System.Drawing.Image.FromFile("C:\temp\myPictureFile.bmp")

    'Print the Image.  'e' is the Print events that the printer provides.  In e is a graphics object on hwich you can draw.
    '10, 10 is the position to print the picture.  
     e.Graphics.DrawImage(myPicture, 10, 10)

    'Clean up
    myPicture.Dispose()
    myPicture = Nothing

    'Tell the printer that there are no more pages to print.  This will cause the document to be finalised and come out of the printer.
    e.HasMorePages = False
End Sub

Again, there are a lot more parameters in DrawImage and the PrintDocument objects.

Michael Rodrigues