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 739635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:25:24+00:00 2026-05-14T08:25:24+00:00

Hey, I need to do my drawing over a panel in C# but without

  • 0

Hey, I need to do my drawing over a panel in C# but without placing my drawing code inside “panel1_Paint”, how can I do that ??
BTW, I’m using WinForms.

Update : I forgot to make something clear, I need not to place my drawing code inside paint handler, because I need to start drawing depending on buttons’ events.

  • 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-14T08:25:24+00:00Added an answer on May 14, 2026 at 8:25 am

    Usually you do all your drawings in the paint event handler. If you want to do any update (if a user clicks on the panel for example) you have to defer that action: you store the required data (coordinates where the user clicked) and force a redraw of the control. This causes the paint event to be fired, where you can then draw things you stored before.

    Another way would be (if you really want to draw outside of the ‘panel1_Paint’ event handler) to draw inside a buffer image, and copy the image to the controls graphics object in the paint event handler.

    Update:

    An example:

    public class Form1 : Form
    {
        private Bitmap buffer;
    
        public Form1()
        {
            InitializeComponent();
    
            // Initialize buffer
            panel1_Resize(this, null);
        }
    
        private void panel1_Resize(object sender, EventArgs e)
        {
            // Resize the buffer, if it is growing
            if (buffer == null || 
                buffer.Width < panel1.Width || 
                buffer.Height < panel1.Height)
            {
                Bitmap newBuffer = new Bitmap(panel1.Width, panel1.Height);
                if (buffer != null)
                    using (Graphics bufferGrph = Graphics.FromImage(newBuffer))
                        bufferGrph.DrawImageUnscaled(buffer, Point.Empty);
                buffer = newBuffer;
            }
        }
    
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            // Draw the buffer into the panel
            e.Graphics.DrawImageUnscaled(buffer, Point.Empty);
        }
    
    
    
        private void button1_Click(object sender, EventArgs e)
        {
            // Draw into the buffer when button is clicked
            PaintBlueRectangle();
        }
    
        private void PaintBlueRectangle()
        {
            // Draw blue rectangle into the buffer
            using (Graphics bufferGrph = Graphics.FromImage(buffer))
            {
                bufferGrph.DrawRectangle(new Pen(Color.Blue, 1), 1, 1, 100, 100);
            }
    
            // Invalidate the panel. This will lead to a call of 'panel1_Paint'
            panel1.Invalidate();
        }
    }
    

    Now the drawn images wont be lost, even after a redraw of the control, because it only draws out the buffer (the image, saved in the memory). Additionally you can draw things anytime a event occurs, by simply drawing into the buffer.

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

Sidebar

Related Questions

Hey gang, I need to know if i need to restart my computer to
Hey, i'm extending some WPF controls, and need to implement some custom events. I've
Hey all, I have something of an interesting requirement for my project. I need
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey, I'm using Levenshteins algorithm to get distance between source and target string. also
Hey so what I want to do is snag the content for the first
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey, I've been developing an application in the windows console with Java, and want
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey peoples, I've been studying Java for a couple of weeks, and have decided

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.