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
FroglegPosted May 3, 2011, 4:37 PM
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;
}
RobertPosted May 3, 2011, 9:48 PM
FroglegPosted May 3, 2011, 9:43 PM
RobertPosted May 3, 2011, 9:23 PM
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.
FroglegPosted May 3, 2011, 7:34 PM
I've sent you my email address so you can send it that way
RobertPosted May 3, 2011, 7:33 PM
that didn't work either
RobertPosted May 3, 2011, 7:28 PM
Bah! Still doesn't work even with AV off.
FroglegPosted May 3, 2011, 7:17 PM
RobertPosted May 3, 2011, 7:07 PM
FroglegPosted May 3, 2011, 7:04 PM
RobertPosted May 3, 2011, 7:00 PM
I don't know why this won't work.
FroglegPosted May 3, 2011, 6:24 PM
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
RobertPosted May 3, 2011, 6:16 PM
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);
}
RobertPosted May 3, 2011, 5:51 PM
Thanks for your help. I am not sure what would be interfering with it, but I will dig deeper.
FroglegPosted May 3, 2011, 5:49 PM
You will have to look deeper into your code to find the problem using breakpoints
RobertPosted May 3, 2011, 5:44 PM
Maybe you can open this one...
FroglegPosted May 3, 2011, 5:37 PM
What program do you use
RobertPosted May 3, 2011, 5:35 PM
FroglegPosted May 3, 2011, 5:33 PM
Paste all your code
RobertPosted May 3, 2011, 5:30 PM
Here is the code for my form again...
FroglegPosted May 3, 2011, 5:20 PM
Anyway try this
RobertPosted May 3, 2011, 5:11 PM
RobertPosted May 3, 2011, 5:10 PM
FroglegPosted May 3, 2011, 4:58 PM
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
RobertPosted May 3, 2011, 4:47 PM
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.
RobertPosted May 3, 2011, 4:28 PM
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.
FroglegPosted May 3, 2011, 4:21 PM
RobertPosted May 3, 2011, 4:15 PM
The print preview dialog is blank.
What loads the image in to the printpreview dialog?
FroglegPosted May 3, 2011, 3:58 PM
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);
}
}