How can I draw a panel in C #?

Hey, I need to make a drawing above the panel in C #, but without putting the drawing code inside "panel1_Paint", how can I do this? BTW, I am using WinForms.

Refresh . I forgot to make something clear, I don’t need to place the drawing code inside the paint handler, because I need to start drawing depending on the events of the buttons.

+6
c # winforms drawing
source share
2 answers

Usually you do all your drawings in a paint event handler. If you want to make some kind of update (if the user clicks on the panel, for example), you should postpone this action: you store the necessary data (the coordinates where the user clicked), and force the control to be redrawn. This will cause the drawing event to fire, and then you can draw the things you saved before.

Another way would be (if you really want to draw outside the panel1_Paint event handler) to draw inside the buffer image and copy the image to the graphic control object in the paint event handler.

Update:

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, inverse images will not be lost even after redrawing the control, since it pulls out a buffer (an image stored in memory). In addition, you can draw things at any time when an event occurs, simply pasting it into the buffer.

+13
source share

Try the following: what happens when you return to your application after using another application that closes part or all of your window?

What you have to do is visualize what you need for a bitmap outside the screen called a buffer. You will display the image whenever you want, in response to anything that changes the image. Then from the paint paint event, you copy this buffer to the panel.

Graphical programming is a lot of fun. It may seem that there are many details to consider, but there are not many of them as soon as you recognize them. But this is the area where the slapdash approach will be demonstrated and can make the application very unreliable and unprofessional.

+1
source share

All Articles