3

I am working on a Windows Form to create a Employee Card Layout. In addition to that I have added a Button named "Print" which will Print the Panel Content. When I run the code It shows Error when Form loads: Form Error

Here is my code:

namespace SimpleReport
{
public partial class EmployeeCardForm : Form
{
    //Declare following Object Variables
    PrintDocument printdoc1 = new PrintDocument();
    PrintPreviewDialog previewdlg = new PrintPreviewDialog();
    Panel pannel = null;

    public EmployeeCardForm()
    {
        InitializeComponent();
        //declare event handler for printing in constructor
        printdoc1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage);
    }

    Bitmap MemoryImage;
    public void GetPrintArea(Panel pnl)
    {
        MemoryImage = new Bitmap(pnl.Width, pnl.Height);
        Rectangle rect = new Rectangle(0, 0, pnl.Width, pnl.Height);
        pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.DrawImage(MemoryImage, 0, 0);
        base.OnPaint(e);
    }
    void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
    {
        Rectangle pagearea = e.PageBounds;
        e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - (this.panel1.Width / 2), this.panel1.Location.Y);
    }

    public void Print(Panel pnl)
    {
        pannel = pnl;
        GetPrintArea(pnl);
        previewdlg.Document = printdoc1;
        previewdlg.ShowDialog();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Print(this.panel1);
    }
  }
}

When I debugged the code I came to know that It crashes on first line of OnPaint Event. Please help me out.

1
  • well you say Bitmap MemoryImage and did it occur to you that the paint event fires way long before you click and MemoryImage is gotta be null at that point. Commented May 16, 2012 at 8:03

2 Answers 2

3

MemoryImage is null before GetPrintArea() has been called.

Try this:

protected override void OnPaint(PaintEventArgs e)
{
    if (MemoryImage != null)
    {
        e.Graphics.DrawImage(MemoryImage, 0, 0);
    }
    base.OnPaint(e);
}

That's only if you don't want to draw it when it is null. Because it's null initially and then you set it in GetPrintArea(). Depending on your circumstance, you could call GetPrintArea() instead of the null check or you could initialise MemoryImage immediately, it all depends on how you want it to work.

Sign up to request clarification or add additional context in comments.

1 Comment

No worries, as long as it's definitely what you want to do when MemoryImage is null.
0

You never set your MemoryImage. In your printpage event, before calling DrawImage add GetPrintArea(pnl)

1 Comment

Did you step through the code to ensure yourself the MemoryImage is being created as intended?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.