Wow, I thought there would be all kinds of information about how to print preview an image from a picturebox control, but I am not having much luck.
I thought what I want to do would be easy. Maybe it is and I am just missing something.
I have a picturebox which contains an image.
I want to view that image in the print preview control.
How can I do this? I thought it would be simple, but I can not find any good examples.
Thanks in advance for any help provided. If more information is needed, please let me know.
Loading

FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 12:37 PM EDT
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
loadImage();
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object o, PrintPageEventArgs e)
{
Point p = new Point(2, 2);
e.Graphics.DrawImage( img, p);
}
public void loadImage()
{
img = Image.FromFile("DSCN4850.jpg");
pbCanvas.Image = img;
}
FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 11:58 AM EDT
public partial class Form1 : Form
{
Image img;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
loadImage();
}
public void loadImage()
{
img = Image.FromFile("tree.jpg");
pictureBox1.Image = img;
}
private void button1_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Point p = new Point(20, 20);
e.Graphics.DrawImage(img, p);
}
}
Robert44 reputationPosted May 3, 2011, 12:15 PM EDT
The print preview dialog is blank.
What loads the image in to the printpreview dialog?
FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 12:21 PM EDT
Robert44 reputationPosted May 3, 2011, 12:28 PM EDT
I changed your code slightly, but I think it should still work. My picturebox is named pbCanvas:
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
loadImage();
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object o, PrintPageEventArgs e)
{
Point p = new Point(2, 2);
e.Graphics.DrawImage(canvasImage, p);
}
public void loadImage()
{
Image img = Image.FromFile("DSCN4850.jpg");
pbCanvas.Image = img;
}
This does change the image in my picturebox, but the printpreview is still blank.
Robert44 reputationPosted May 3, 2011, 12:47 PM EDT
Image img;
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
loadImage();
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object o, PrintPageEventArgs e)
{
Point p = new Point(2, 2);
e.Graphics.DrawImage(img, p);
}
public void loadImage()
{
img = Image.FromFile("DSCN4850.jpg");
pbCanvas.Image = img;
}
It does the same exact thing. Changes the image in pbCanvas, but the printpreview is still blank.
FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 12:58 PM EDT
where is "DSCN4850.jpg" on your computer
To work with the above code it needs to be in the bin\debug folder of your project
Robert44 reputationPosted May 3, 2011, 1:10 PM EDT
Robert44 reputationPosted May 3, 2011, 1:11 PM EDT
FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 1:20 PM EDT
Anyway try this
Robert44 reputationPosted May 3, 2011, 1:30 PM EDT
Here is the code for my form again...
FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 1:33 PM EDT
Paste all your code
Robert44 reputationPosted May 3, 2011, 1:35 PM EDT
FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 1:37 PM EDT
What program do you use
Robert44 reputationPosted May 3, 2011, 1:44 PM EDT
Maybe you can open this one...
FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 1:49 PM EDT
You will have to look deeper into your code to find the problem using breakpoints
Robert44 reputationPosted May 3, 2011, 1:51 PM EDT
Thanks for your help. I am not sure what would be interfering with it, but I will dig deeper.
Robert44 reputationPosted May 3, 2011, 2:16 PM EDT
I am loading a new form with the image from the picturebox and the image does show on the new form. That tells me that the image is not blank. But it still does not show up in the printpreview dialog. I don't know what is wrong.
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
//show image on new form
Form tmpForm = new Form();
tmpForm.BackgroundImage = pbCanvas.Image;
tmpForm.ShowDialog();
if (pbCanvas.Image == null)
{
MessageBox.Show("image blank");
}
else
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
}
private void printDocument1_PrintPage(object o, PrintPageEventArgs e)
{
Point p = new Point(2, 2);
e.Graphics.DrawImage(img, p);
}
FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 2:24 PM EDT
When you zip your project, can you then unzip it ?
Windows has inbuilt zip capabilities (from xp on)
So see if you can zip it with another program
Robert44 reputationPosted May 3, 2011, 3:00 PM EDT
I don't know why this won't work.
FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 3:04 PM EDT
Robert44 reputationPosted May 3, 2011, 3:07 PM EDT
FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 3:17 PM EDT
Robert44 reputationPosted May 3, 2011, 3:28 PM EDT
Bah! Still doesn't work even with AV off.
Robert44 reputationPosted May 3, 2011, 3:33 PM EDT
that didn't work either
FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 3:34 PM EDT
I've sent you my email address so you can send it that way
Robert44 reputationPosted May 3, 2011, 5:23 PM EDT
If anyone else has this problem, the solution was to remove the hand typed PrintPage handler and generate the handler from the printdocument in the component tray by double-clicking on it. Then place the same code inside of that generated handler.
Apparently, it is a bug in VS 2010.
FroglegRank #1,020 · 1.9K reputationPosted May 3, 2011, 5:43 PM EDT
Robert44 reputationPosted May 3, 2011, 5:48 PM EDT