This is something I am desperately trying to figure out. I am attaching the portion of the code I am having problem with, along with the point where the exception was raised. Any help would be appreciated.
private void button1_Click(object sender, EventArgs e)
{
try
{
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog(); // instantiate new print preview dialog
printPreviewDialog1.Document = this.printDocument1;
printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
printPreviewDialog1.SetBounds(20, 20, this.Width, this.Height);
printPreviewDialog1.ShowDialog(); // Show the print preview dialog, uses print page event to draw preview screen
}
catch (Exception exp)
{
//System.Console.WriteLine(exp.Message.ToString());
MessageBox.Show("Exception caught",exp.ToString());
}
}
RectangleF GetScaledRectangle(float x, float y, RectangleF r)
{
RectangleF result = new RectangleF(r.X * x, r.Y * y, r.Width * x, r.Height * y);
return result;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
this.Focus();
Graphics g = e.Graphics;
// RectangleF srcRect = new Rectangle(0, 0, this.LargerImage.Width,
// LargerImage.Height);
int a = this.BackgroundImage.Width;
int b = this.BackgroundImage.Height;
Rectangle srcRect = new Rectangle(0, 0, a, b);
RectangleF cr = srcRect;
int nWidth = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Width;
int nHeight = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Height;
RectangleF destRect = new RectangleF(0, 0, nWidth, nHeight);
g.DrawImage(this.BackgroundImage, destRect, srcRect, GraphicsUnit.Pixel);
// g.DrawImage(this.LargerImage, destRect, srcRect, GraphicsUnit.Pixel);
float scalex = destRect.Width / this.BackgroundImage.Width;
float scaley = destRect.Height / this.BackgroundImage.Height;
Pen aPen = new Pen(Brushes.Black, 1);
for (int i = 0; i < this.Controls.Count; i++)
{
// draw logo
if (Controls[i].GetType() == this.pictureBox1.GetType())
{
if (pictureBox1.Image != null)
{
GraphicsUnit gu = GraphicsUnit.Pixel;
RectangleF scaledRectangle = GetScaledRectangle(scalex, scaley, pictureBox1.Bounds);
Image myImage = (Image)pictureBox1.Image.Clone();
g.DrawImage(myImage, scaledRectangle, pictureBox1.Image.GetBounds(ref gu), GraphicsUnit.Pixel);
}
}
Exception is raised at the point underlined in red. The exception raised is NullReferenceException unhandled. I am at a loss. Please help.