PrintDocument1.Print()
PrintDocument1.Dispose()
I have these two lines withing a sub to print a document; part of the process is that a picture is created, which happens just fine, and then the page prints just fine as well. The problem is that after the page is printed, I want to be able to delete the picture that was created in my temp folder; I thought that the .dispose() line would do that for me. I had to add that in there so that when the next page prints, I would be able to have a new picture on that page. The problem is that the picture is still being used after it prints, and is never released. I tried deleting the file manually after I printed a sample, but that didn't work either as it was in use.
For a better understanding of what I am doing, here is my code, and like I say, it works fine. I am curious if this is how you would do it or if there is a better way?
I work at a lab that analyses water samples, and then we can print out the samples with the data at the bottom, and a picture of the graph at the top. When the scale factor is very large, we want to be able to 'autoscale' it (zoom in on the 'x' axis) to make sure it is drawn correctly. Sometimes an auto scale is needed, othertimes it is not. Here is the basic code that I have:
Public Class frmPrintGraphsNew
Dim printAutoScale As Boolean
Dim printGraph As Boolean
Private Sub cmdPrintGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrintGraph.Click
printGraph = True
PrintDocument1.Print()
PrintDocument1.Dispose()
End Sub
Private Sub cmdPrintAutoScale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrintAutoScale.Click
printAutoScale = True
PrintDocument1.Print()
PrintDocument1.Dispose()
End Sub
Private Sub cmdPrintAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrintAll.Click
printAutoScale = True
printGraph = True
PrintDocument1.Print()
PrintDocument1.Dispose()
PrintDocument1.Print()
PrintDocument1.Dispose()
End Sub
Private Sub cmdExitPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExitPrint.Click
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim autoScaleFileName As String
Dim graphFileName As String
Dim temp As String
temp = Sample_File_Name(frmMain.txtSampleNumber.Text)
autoScaleFileName = Mid(temp, 1, Len(temp) - 3) & "HGC"
graphFileName = Mid(temp, 1, Len(temp) - 3) & "HGL"
If printAutoScale = True Then
printPage(e, autoScaleFileName, "autoscale")
Exit Sub
End If
If printGraph = True Then
printPage(e, graphFileName, "graph")
End If
End Sub
Private Sub printPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal fileToPrint As String, ByVal graphType As String)
'this is where I have all sorts of cool looking code to print my sample
end sub
End Class
PrintDocument1.Dispose()
I have these two lines withing a sub to print a document; part of the process is that a picture is created, which happens just fine, and then the page prints just fine as well. The problem is that after the page is printed, I want to be able to delete the picture that was created in my temp folder; I thought that the .dispose() line would do that for me. I had to add that in there so that when the next page prints, I would be able to have a new picture on that page. The problem is that the picture is still being used after it prints, and is never released. I tried deleting the file manually after I printed a sample, but that didn't work either as it was in use.
For a better understanding of what I am doing, here is my code, and like I say, it works fine. I am curious if this is how you would do it or if there is a better way?
I work at a lab that analyses water samples, and then we can print out the samples with the data at the bottom, and a picture of the graph at the top. When the scale factor is very large, we want to be able to 'autoscale' it (zoom in on the 'x' axis) to make sure it is drawn correctly. Sometimes an auto scale is needed, othertimes it is not. Here is the basic code that I have:
Public Class frmPrintGraphsNew
Dim printAutoScale As Boolean
Dim printGraph As Boolean
Private Sub cmdPrintGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrintGraph.Click
printGraph = True
PrintDocument1.Print()
PrintDocument1.Dispose()
End Sub
Private Sub cmdPrintAutoScale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrintAutoScale.Click
printAutoScale = True
PrintDocument1.Print()
PrintDocument1.Dispose()
End Sub
Private Sub cmdPrintAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrintAll.Click
printAutoScale = True
printGraph = True
PrintDocument1.Print()
PrintDocument1.Dispose()
PrintDocument1.Print()
PrintDocument1.Dispose()
End Sub
Private Sub cmdExitPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExitPrint.Click
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim autoScaleFileName As String
Dim graphFileName As String
Dim temp As String
temp = Sample_File_Name(frmMain.txtSampleNumber.Text)
autoScaleFileName = Mid(temp, 1, Len(temp) - 3) & "HGC"
graphFileName = Mid(temp, 1, Len(temp) - 3) & "HGL"
If printAutoScale = True Then
printPage(e, autoScaleFileName, "autoscale")
Exit Sub
End If
If printGraph = True Then
printPage(e, graphFileName, "graph")
End If
End Sub
Private Sub printPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal fileToPrint As String, ByVal graphType As String)
'this is where I have all sorts of cool looking code to print my sample
end sub
End Class