Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:35:47+00:00 2026-06-03T23:35:47+00:00

I am working on a Windows Form to create a Employee Card Layout. In

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:35:48+00:00Added an answer on June 3, 2026 at 11:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with a C# project using System.Windows.Form to create the GUI, I have
I have modified a working Windows service that had always been starting beforehand. After
I am working with Windows Forms, is it possible to create a window which
I am working Windows form Application in c#.I like to create crystal report from
I have a Windows form which hosts a custom User Control. This User Control
I have a method that creates a bound with a windows form and also
I am currently working on a C# windows form with linq-to-sql that selects a
I'm creating a .vdproj setup for a simple Windows form. I have added a
I have a jquery-ui-dialog that contains a <form> which contains a <input type=file >
I'm currently working on an application that uses a Windows Form GUI. The main

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.