How to print contents of PictureBox?

Joined
Oct 20, 2006
Messages
21
Programming Experience
Beginner
Hello all!

I'm working on a barcode project in VB.NET 2005. I'm not a terribly good programmer and need a little help here. What this program does thus far is take input from a text field and passes it to a module where the input is converted into binary and printed as lines to a picture box on the screen. What I would like to do is then print whatever is in the box to a page. I added the print document tool. In the area of the code where the system generated code is I added the following procedure

VB.NET:
Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
PreviewMe.ShowDialog()
End Sub
 
Private Sub PrintMe_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintMe.PrintPage
Dim MyGraphicsPage As Graphics = e.Graphics
MyGraphicsPage.PageUnit = GraphicsUnit.Inch
MyGraphicsPage.DrawImage(PictureBox1.Image, 1, 1, PictureBox1.Width, PictureBox1.Height)
End Sub
 
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
PrintMe.Print()
End Sub
This doesn’t seem to do anything. I get an error when I try to print and "No pages found" when I try to preview. Any suggestions?
 
Last edited by a moderator:
When you say this:
printed as lines to a picture box on the screen
do you mean that you're using a Graphics object to draw the text on the PictureBox? If so then that has no relationship whatsoever to the Image property of the PictureBox. Unless you assign an Image object to the Image property then the Image property is Nothing. If you want to use a Graphics object to draw on the PictureBox and you want to be able to print the result then you should use the very same code to draw on the printing surface, e.g.
VB.NET:
Private str As String = "Hello World"

Private Sub PictureBox1_Paint(ByVal sender As Object, _
                              ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    'Write the string on the PictureBox.
    Me.WriteString(e.Graphics)
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
                                     ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    'Write the string on the printing surface.
    Me.WriteString(e.Graphics)
End Sub

Private Sub WriteString(ByVal g As Graphics)
    'Draw the text on whatever surface the Graphics object refers to.
    g.DrawString(Me.str, Me.Font, Brushes.Black, 1, 1)
End Sub
Alternatively, you can create a Bitmap object, draw on that whatever you previously drew on the PictureBox and assign that to the PictureBox's Image property. It will still look the same in the PictureBox and you can then just print the Image property. That's how you can use a PictureBox to create a simple drawing program with an undo feature. When you want to make the drawing permanent you draw on the underlying Bitmap instead of the PictureBox itself. In your case I'd think my previous example would be easier though. Note that if you aren't going to be using the Image property then a PictureBox is pointless. You may as well just use a Panel if you need a border or just the form itself if you don't.
 
This is the code from the module that prints to the picturebox

'*
'* Draw the BarCode
'*
For K = 1 To Len(strEANBin)
If Mid(strEANBin, K, 1) = "1" Then
objPicBox.CreateGraphics.FillRectangle(New System.Drawing.SolidBrush(objPicBox.ForeColor), sngPosX, sngY1, sngScaleX, sngPosY)
End If
sngPosX = sngX1 + (K * sngScaleX)
Next K
 
Don't hijack the Picturebox control like you do with that Creategraphics method. Instead draw to a bitmap image and display that image in picturebox, you can also print that image.
VB.NET:
Dim bmp As New Bitmap(100, 200)
Dim g As Graphics = Graphics.FromImage(bmp)
g.FillRectangle(..........)
g.Dispose()
PictureBox1.Image = bmp
 
Back
Top