[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

printpage event apparently not firing

I am trying to programatically print a code generated image.  The graphics is populated and I can save the .bmp to disk and it is correct.  However, when I send it to the printer, I get a blank page.  Apparently this is occurning because the printpage event is not firing.
I say that the event is not firing because when I place        
objBitmap.Save("c:\testing.bmp")
in the Sub pd_PrintPage it does not happen.
Private objBitmap As New Bitmap(765, 800)
 
    Private Sub butPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butPrint.Click
        Try
            Dim objGraphics As Graphics
            Dim objFont As New Font("Arial", 12, FontStyle.Regular)
            objGraphics = Graphics.FromImage(objBitmap)
            objGraphics.DrawImage(objBitmap, 0, 0)
            objGraphics.DrawString(str1.ToString, objFont, Brushes.Black, 50, 50)
            objGraphics.DrawString(str2.ToString, objFont, Brushes.Black, 90, 100)
            objGraphics.DrawString(str3.ToString, objFont, Brushes.Black, 90, 240)
            Dim objPoint As Drawing.Point
            objPoint.X = "90"
            objPoint.Y = "200"
            objGraphics.DrawImageUnscaled(image1, objPoint)
            objGraphics.Save()
            Dim objDoc As New PrintDocument
            objDoc.PrinterSettings.PrinterName = prnName
            AddHandler objDoc.PrintPage, AddressOf Me.pd_PrintPage
            objDoc.Print()
            lblStatus.Text = "Printing Complete"
        Catch ex As Exception
            lblStatus.Text = ex.Message
        End Try
 
    End Sub
 
    Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs) _
        Handles objDoc.PrintPage
 
        ' Draw a picture.
        ev.Graphics.DrawImage(objBitmap, _
        ev.Graphics.VisibleClipBounds)
 
        ' Indicate that this is the last page to print.
        ev.HasMorePages = False
    End Sub

Open in new window

0
zane_o
Asked:
zane_o
  • 2
  • 2
1 Solution
 
PaulHewsCommented:
You need to declare your PrintDocument at the form/class level:
 Private objBitmap As New Bitmap(765, 800)
    Private WithEvents objDoc As New PrintDocument
 
    Private Sub butPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            
 
            Dim objGraphics As Graphics
            Dim objFont As New Font("Arial", 12, FontStyle.Regular)
            objGraphics = Graphics.FromImage(objBitmap)
            objGraphics.DrawImage(objBitmap, 0, 0)
            objGraphics.DrawString(str1.ToString, objFont, Brushes.Black, 50, 50)
            objGraphics.DrawString(str2.ToString, objFont, Brushes.Black, 90, 100)
            objGraphics.DrawString(str3.ToString, objFont, Brushes.Black, 90, 240)
            Dim objPoint As Drawing.Point
            objPoint.X = 90
            objPoint.Y = 200
            objGraphics.DrawImageUnscaled(image1, objPoint)
            objGraphics.Save()
            objDoc.PrinterSettings.PrinterName = prnName
            objDoc.Print()
            lblStatus.Text = "Printing Complete"
        Catch ex As Exception
            lblStatus.Text = ex.Message
        End Try
 
    End Sub
 
    Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs) _
        Handles objDoc.PrintPage
 
        ' Draw a picture.
        ev.Graphics.DrawImage(objBitmap, _
        ev.Graphics.VisibleClipBounds)
 
        ' Indicate that this is the last page to print.
        ev.HasMorePages = False
    End Sub

Open in new window

0
 
PaulHewsCommented:
One typo.
Private Sub butPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Should be:

Private Sub butPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butPrint.Click
0
 
zane_oAuthor Commented:
Sorry I didn't include this in the original post, but I have
    Private WithEvents objDoc As PrintDocument
in the code already.
0
 
zane_oAuthor Commented:
OK. That did the trick.  Thanks.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now