我正在尝试打印新创建的 png 文件。 我正在使用文件系统观察器在创建文件并获取它的路径时做出反应。
Private Sub FileSystemWatcher1_Created(sender As Object, e As FileSystemEventArgs) Handles FileSystemWatcher1.Created
Dim MyFile As String = e.FullPath.ToString
Try
AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintImage
PrintDocument1.Print()
Catch ex As Exception
MsgBox(ex, MsgBoxStyle.Critical, "Error during Print")
End Try
End Sub
但是当我想打印它时,我不知道如何直接从 MSDN 到代码的路径,就像此代码 Me.PrintImage 和 Me.PrintImage(MyFile) 返回错误
WithEvents PrintDocument1 As New PrintDocument
Private Sub PrintImage(ByVal sender As Object, ByVal ppea As PrintPageEventArgs, MyFile As String)
ppea.Graphics.DrawImage(Image.FromFile(MyFile), ppea.Graphics.VisibleClipBounds)
ppea.HasMorePages = False
End Sub
我怎样才能将我的文件定向到此打印服务?我发现的代码基于文件的静态路径,并且不在文件系统观察器中,我似乎找不到将它们绑定在一起的方法。 请帮忙!
请考虑以下事项:
Private Sub PrintImage(ByVal sender As Object, ByVal ppea As PrintPageEventArgs, MyFile As String)
MyFile
:Public Class Form1
Private MyFile As String
End Class
现在您可以在
FileSystemWatcher1_Created
事件中分配其值,并在 PrintImage
事件中读取该值。要了解更多信息:如何:控制变量的范围 (Visual Basic)
Public Class Form1
Private MyFile As String
Private PrintDocument1 As PrintDocument
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PrintDocument1 = New PrintDocument
AddHandler PrintDocument1.PrintPage, AddressOf PrintImage
End Sub
Private Sub PrintImage(sender As Object, e As PrintPageEventArgs)
'...
End Sub
End Class
或者只是使用 WithEvents 和 Handles 子句,就像在
FileSystemWatcher1_Created
事件中一样。
Public Class Form1
Private MyFile As String
Private WithEvents PrintDocument1 As PrintDocument
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PrintDocument1 = New PrintDocument
End Sub
Private Sub PrintImage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
'...
End Sub
End Class
或者使用设计器添加
PrintDocument
组件的实例并订阅其事件。就像 FileSystemWatcher
组件一样。
把它们放在一起,你应该得到类似的东西:
Public Class Form1
Private MyFile As String
Private WithEvents PrintDocument1 As PrintDocument
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PrintDocument1 = New PrintDocument
End Sub
Private Sub FileSystemWatcher1_Created(sender As Object, e As FileSystemEventArgs) Handles FileSystemWatcher1.Created
MyFile = e.FullPath
PrintDocument1.Print()
End Sub
Private Sub PrintImage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
If Not File.Exists(MyFile) Then
Return
End If
Try
Using bmp = New Bitmap(New MemoryStream(File.ReadAllBytes(MyFile)))
e.Graphics.DrawImage(bmp, e.Graphics.VisibleClipBounds)
End Using
Catch ex As ArgumentException
Console.WriteLine("Not Image!")
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
MyBase.OnFormClosed(e)
PrintDocument1.Dispose()
End Sub
End Class
恕我直言,更好的方法是将创建的文件收集在列表中并将它们作为一个打印作业发送到打印机。例如,当您将多个文件粘贴到
FileSystemWatcher.Path
中时很有用。为每个文件引发 FileSystemWatcher.Created
事件,并为每个文件调用前面代码中的 PrintDocument.Print
方法。我们可以将打印调用减少到一次来完成相同的任务。
下面的代码是一个快速的 Lambda-Expression-Way 示例。
Imports System.IO
Imports System.Windows.Forms
Imports System.Drawing.Printing
Imports System.Collections.Generic
Public Class Form1
Private ReadOnly watcher As FileSystemWatcher
Private ReadOnly timer As Timer 'System.Windows.Forms.Timer
Private ReadOnly printDoc As PrintDocument
Private MyFiles As Queue(Of String)
Sub New()
InitializeComponent()
MyFiles = New Queue(Of String)
watcher = New FileSystemWatcher With {
.Path = "PathToWatch",
.EnableRaisingEvents = True,
.SynchronizingObject = Me
}
AddHandler watcher.Created,
Sub(s, e)
If Not MyFiles.Contains(e.FullPath) Then
MyFiles.Enqueue(e.FullPath)
timer.Start()
End If
End Sub
timer = New Timer With {.Interval = 200}
AddHandler timer.Tick,
Sub(s, e)
timer.Stop()
printDoc.Print()
End Sub
printDoc = New PrintDocument
AddHandler printDoc.PrintPage,
Sub(s, e)
Dim imgFile = MyFiles.Dequeue
If Not File.Exists(imgFile) Then
Return
End If
Try
Using bmp = New Bitmap(New MemoryStream(File.ReadAllBytes(imgFile)))
e.Graphics.DrawImage(bmp, e.Graphics.VisibleClipBounds)
End Using
Catch ex As ArgumentException
Console.WriteLine("Not Image!")
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
e.HasMorePages = MyFiles.Count > 0
End Sub
End Sub
Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
MyBase.OnFormClosed(e)
watcher.Dispose()
timer.Dispose()
printDoc.Dispose()
End Sub
End Class