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

  • SEARCH
  • Home
  • 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 6873841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:07:12+00:00 2026-05-27T04:07:12+00:00

I have written an event handler method and attached it to the Paint event

  • 0

I have written an event handler method and attached it to the Paint event of a Form (just the main window). This event sends a PaintEventArgs containing a property called Graphics, which is an instance of System.Drawing.Graphics.

Here is the code I’m using:

private void Form1_Paint(object sender, PaintEventArgs e) {

    Bitmap bm = new Bitmap("fruit-dealer-full.jpg");

    Graphics g1 = this.CreateGraphics();
    Graphics g2 = e.Graphics;

    // g1.DrawImage(bm, 0, 0, this.Width, this.Height);
    // g1.DrawRectangle(
    //       Pens.White, 10.0f, 10.0f, this.Width - 200, this.Height - 200);

    g2.DrawImage(bm, 0, 0, this.Width, this.Height);
    g2.DrawRectangle(
           Pens.White, 10.0f, 10.0f, this.Width - 200, this.Height - 200);

}

Ultimately I just want to gain a better understanding of what’s happening here, but specifically I have these three questions:

  1. Why does g1 redraw the image in the whole window, while g2 only draws the new portion, even if I call g2.Clear() before drawing?
  2. Why, with either Graphics object, is the image only redrawn when the window increases in size, and not when it is made smaller?
  3. If PaintEventArgs.Graphics can (or should) not be used for drawing, what is it used for? I would imagine it just prevents you from having to create a new Graphics instance if the form doesn’t need to be redrawn; is there more to it that I’m missing?
  • 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-05-27T04:07:13+00:00Added an answer on May 27, 2026 at 4:07 am

    You .NET WinForms kids really should learn the Win32 API. Pick up a copy of Petzold already!

    Why does g1 redraw the image in the whole window, while g2 only draws the new portion, even if I call g2.Clear() before drawing?

    Presumably g2 is a wrapper around a device context received from BeginPaint. I imagine WinForms wraps PAINTSTRUCT::rcPaint for you – this variable describes the area to be painted. This is expected behaviour – rather than burning CPU cycles redrawing your entire window every single time another window overlaps it by just one pixel, you can redraw … just the one pixel!

    g2.Clear is presumably limited by rcPaint.

    g1 is probably a GetDC for the window – which gives you the entire surface to draw on.

    Why, with either Graphics object, is the image only redrawn when the window increases in size, and not when it is made smaller?

    The underlying window class probably doesn’t have CS_HREDRAW or CS_VREDRAW window styles. Without these, there’s no reason for the default behaviour to request you redraw the window when it gets smaller: Windows knows what the entire window looks like, it can clip the unwanted bits away. This is unlike when the window gets bigger, it doesn’t know what to draw in the new area.

    If PaintEventArgs.Graphics can (or should) not be used for drawing, what is it used for? I would imagine it just prevents you from having to create a new Graphics instance if the form doesn’t need to be redrawn; is there more to it that I’m missing?

    It’s used for drawing. Unless you have some complex drawing requirements, you can use the PaintEventArgs.Graphics minimize the amount of painting that goes on to your window. (As above – this is a huge saver of CPU cycles and it’s probably a simple wrapper over BeginPaint and EndPaint – which is how drawing to the client area is meant to be done.)

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

Sidebar

Related Questions

I have written this code which accesses the Delete method on Web Service x,
I have form which is opened using ShowDialog Method. In this form i have
I am developing an application which requires to add Calendar event. I have written
Just how much slower are events? I have written a streaming XML parser (that
I'm attempting to add an event handler for every control on my form. The
I have an interface, written in C#, defined as this : public interface IWidget
I have written a Magento module to listen for the OrderSave event and perform
I am trying to design a windows form application. Now I have written an
I have a button which stores data written in the EditText form above it
I have written a method trial for button btnTrial1 : public void trial(object sender,

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.