Print the contents of a picturebox  
Author Message
crogenicdude





PostPosted: Visual Basic Express Edition, Print the contents of a picturebox Top

Hi all! I'm a bit new to vb .net 2005 express. But even other than that, I always seem to get stuck when it comes to printing!

I have an image on my picturebox control (whatever it is!)

Now, all I want to do is associate this picturebox image to a printdocument object and then use a printdialog control to show it to me and allow me to print it.

So far I have a picturebox control (picturebox1) and a button (button1) control on the form. And all I want it to do is just print the damn picture in the picturebox to a printer once the button1 is clicked!

Can anyone please help me!



Visual Studio Express Editions5  
 
 
Tall Dude





PostPosted: Visual Basic Express Edition, Print the contents of a picturebox Top

Here's some 'quick and dirty' code I had handy:

Public Class Form1

' A 'PrintDocument' is not something to be printed.

' It is a collection of settings, margins, portrait

' landscape, resolutions, default printer etc.

' What needs to be printed, is introduced in the PrintDocument's

' PrintPage event handler subroutine (method).

Public pd As New Printing.PrintDocument

Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

' The PrintDocument, PrintPage event is called

' by the printing event and/or the printpreview event

AddHandler pd.PrintPage, AddressOf printit

End Sub

Private Sub printit(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

' For longer text, see my long example on how to calculate how much

' is printed on each page and how to set 'has more pages' to true.

' By default, 'has more pages' is False

' e.Graphics.DrawString("Print or Preview Text", Me.Font, Brushes.Black, 0, 0)

e.Graphics.DrawImage(PictureBox1.Image, 0, 0, PictureBox1.Width, PictureBox1.Height)

' If 'has more pages were true, 'printit' would exit through

' the 'end sub' and be immediately be called again by Visual Basic

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

' Print with button 1

pd.Print()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

' preview with button 2

Dim prp As New PrintPreviewDialog

prp.Document = pd

prp.ShowDialog()

End Sub

End Class



 
 
carlb28





PostPosted: Visual Basic Express Edition, Print the contents of a picturebox Top

Thanks for the code, but how do you print an drawimage as well as drawstring

ive got a picturebox of a barcode which the lines are a graphic and the numbers is text, how do you print both parts

thanks in advance

Carl



 
 
Tall Dude





PostPosted: Visual Basic Express Edition, Print the contents of a picturebox Top

Well, the drawimage statement above should be drawing the complete

picturebox image, no matter what it is.

If you are also using the picturebox paint event to draw some text in the

picturebox , then you will have to add the same drawstring code you have in the paint event

to the printdocument print event.

If you are drawing everything in the picturebox then there are ways to write the code

in a 'shareable' way.

Let me know if this helped