使用VB6和/或.NET打印照片

时间:2010-03-10 00:47:58

标签: vb.net vb6

是否有人使用Visual Basic或.NET框架打印照片(BMP或TIFF或JPEG)有任何代码建议或示例?

1 个答案:

答案 0 :(得分:3)

VB6和.NET处理打印方式完全不同。这些例子是最低限度的,只是为了让您了解这个过程。

在VB6中,您可以逐步控制打印机:

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

瞧,你的照片将来自默认打印机。 PaintPicture方法接受宽度,高度和一些其他参数来帮助使图像适合,而Printer对象为您提供有关打印机的各种信息。

在.Net中,它是另一种方式。您开始打印,打印机将为每个页面引发一个事件,直到您告诉它停止为止。每个页面事件都会为您提供图形对象,您可以使用所有标准的System.Drawing类和方法进行绘制:

'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

同样,DrawImage和PrintDocument对象中还有更多参数。