Print image off a userform

Fin Fang foom

Member
Joined
Dec 23, 2005
Messages
8
Programming Experience
Beginner
Does anyone has a code that I could print a picture from a userform ?

I have this userform that I created. It woks like this when putting the file name in the Tex Box and hit enter it loads the image But I just can't figuer out how to print out the picture including the name.

Any help ?
 
This should give you something to start with, finetune it yourself:
I started a fresh Windows Application project, in Designer View added the controls: Label, Picturebox, PrintDocument, PrintPreviewDialog (you can use the PrintDialog also).
Still in Designer View I just preset the label to an existing image (and had it loaded in form load event for this example). Then selected PrintDocument1 in PrintPreviewDialog's Document property.
In Code Editor I added this code:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
[/SIZE][SIZE=2]PictureBox1.Image = Image.FromFile(Label1.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click
PrintPreviewDialog1.ShowDialog()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PrintDocument1_PrintPage([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Drawing.Printing.PrintPageEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] PrintDocument1.PrintPage
e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, 10, 10)
e.Graphics.DrawImage(PictureBox1.Image, 30, 30)
e.HasMorePages = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
Starting up the project now loads the image into the picturebox, clicking the button shows printpreview with image title written on top of page and image just below. You can click the print icon from the printpreview and the page will print to default printer.
 
If you can take a look at this. I just can't get your printing to work.

Do you know what I'm doing worng ?
 

Attachments

  • Picture Display.zip
    29 KB · Views: 36
You just forgot to set the Document property of the PrintPreviewDialog to PrintDocument1. Select it in Designer View, or add it programatically.
( Me.PrintPreviewDialog1.Document = Me.PrintDocument1 )

Also, since you are using a TextBox and my example was a Label, you could edit this line:
e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, 10, 10)
 
I enter the file name in the text box and I enter and the picture pops up so thats fine. The I hit the button for the print preview and thats comes up then I go to the left corner of the print preview screen and hit the printer icon and nonthing happen.

Why is that ?

Am I missing something ?
 
Thank You it works great. Thank You so much!

One more think the code that JohnH provided :

VB.NET:
PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
PictureBox1.Image = Image.FromFile(Label1.Text)
EndSub

I created the label control and I inserted the code to the Form1.
But name of the file does not appear. I would like to have it when I print out the picture the name of the picture in under center footer of the image.

Please let me know I've doing this for a week now so any help will be great.

Any Ideas ?
 
I told you this before:
JohnH said:
Also, since you are using a TextBox and my example was a Label, you could edit this line:
e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, 10, 10)

Use measurements in PrintDocument1.DefaultPageSettings to position it where you want on page
 
Ok got it Thanks!

I'm so new to this VB.Net stuff.

I change this:

e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, 10, 10)

To this:

e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, 300, 400)

But how can the I change it to be Lansdscape ?
 
Back
Top