Solved

printpage event apparently not firing

Posted on 2007-11-21
4
431 Views
Last Modified: 2013-11-07
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
Comment
Question by:zane_o
  • 2
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 250 total points
ID: 20330503
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 20330727
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
 
LVL 6

Author Comment

by:zane_o
ID: 20331231
Sorry I didn't include this in the original post, but I have
    Private WithEvents objDoc As PrintDocument
in the code already.
0
 
LVL 6

Author Comment

by:zane_o
ID: 20331384
OK. That did the trick.  Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now