Reputation: 593
I am using panel to draw shapes like circle, but the problem is that when I change tab or minimize the program & then maximize the program, then every thing drawn removed. Can any one tell me what's the reason?
Upvotes: 0
Views: 25523
Reputation: 2911
Buffering like this: (You could skip the redraw on form resize by forcing a larger bitmap in form load.)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication10
{
public partial class Form1 : Form
{
Bitmap bitmap = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Redraw();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
OnPaint(e);
}
protected override void OnPaint(PaintEventArgs e)
{
if (bitmap == null)
{
base.OnPaint(e);
}
else
{
e.Graphics.DrawImageUnscaled(bitmap, 0, 0);
}
}
private void Form1_Resize(object sender, EventArgs e)
{
Redraw();
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
Redraw();
this.Invalidate();
}
private void Redraw()
{
if (bitmap != null)
{
bitmap.Dispose();
bitmap = null;
}
bitmap = new Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.Clear(Color.White);
// This is where you could use a List<Shape> etc...
g.DrawEllipse(Pens.Black, new Rectangle(10, 10, 20, 20));
g.DrawEllipse(Pens.Black, new Rectangle(20, 30, 20, 20));
g.DrawEllipse(Pens.Black, new Rectangle(50, 90, 30, 20));
}
}
}
}
Upvotes: 0
Reputation: 81655
Since you didn't post any code, you leave all of your readers guessing. My guess is that you are most likely using CreateGraphics, which is probably a mistake.
Your panel's paint event should look something like this:
private void panel1_Paint(object sender, PaintEventArgs e) {
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.FillEllipse(Brushes.Red, new Rectangle(10, 10, 32, 32));
}
You do all of your drawing in the paint event. To force a refresh, just call panel1.Invalidate()
.
If drawing to a bitmap, you could handle it like this:
Bitmap bmp = new Bitmap(500, 500);
private void button1_Click(object sender, EventArgs e) {
using (Graphics g = Graphics.FromImage(bmp)) {
g.SmoothingMode = SmoothingMode.AntiAlias;
g.Clear(Color.White);
g.FillEllipse(Brushes.Red, new Rectangle(10, 10, 32, 32));
}
panel1.Invalidate();
}
private void panel1_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawImage(bmp, new Point(0, 0));
}
Upvotes: 7
Reputation: 57593
The reason is that control is invalidated and then redrawn.
You should catch Paint
event and redraw everything you need inside panel.
This event is fired every time a control has to be (partially o completely) redrawn.
EDITED after user comment:
When your user draws something, you could store the shape (type and various coords) in a List
and, on Paint
event, redraw everything contained in the list from top to bottom.
Upvotes: 0
Reputation: 24283
Anything you draw is (by default) not persistant, and can get lost of anythign invalidates part of the window (cover it up and uncover, minimise, UAC prompt, etc.
To get around this, you need to remember what you need to draw and redraw it in the Paint
event.
Upvotes: 0