C # paint program flickering

I am trying to make a simple paint program in C #, but it continues to flicker when I draw, for example, I need some kind of double buffering, but I donโ€™t know how to do it.

I draw Panel and I use Bitmap to store graphics.

Here is my code:

 public partial class Form1 : Form { private Bitmap drawing; private bool leftDown = false; private int prevX; private int prevY; private int currentX; private int currentY; public Form1() { InitializeComponent(); drawing = new Bitmap(panel1.Width, panel1.Height); } private void panel1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { leftDown = true; prevX = eX; prevY = eY; } } private void panel1_MouseMove(object sender, MouseEventArgs e) { if (leftDown) { Graphics g = Graphics.FromImage(drawing); currentX = eX; currentY = eY; g.DrawLine(new Pen(Color.Black), new Point(currentX, currentY), new Point(prevX, prevY)); panel1.Invalidate(); prevX = currentX; prevY = currentY; g.Dispose(); } } private void panel1_MouseUp(object sender, MouseEventArgs e) { leftDown = false; } private void panel1_MouseLeave(object sender, EventArgs e) { leftDown = false; } private void panel1_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawImageUnscaled(drawing, 0, 0); } } 
+4
source share
4 answers

You should not only turn DoubleBuffered into true , but also use a PictureBox instead of a Panel and draw it. This should solve your problem :).

+3
source

You need to subclass Panel for this, because you need to override some things. A Panel how it should work:

 class DoubleBufferedPanel : Panel { public DoubleBufferedPanel() : base() { this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffered | ControlStyles.Opaque | ControlStyles.OptimizedDoubleBuffer, true); } public override void OnPaint(PaintEventArgs e) { // Do your painting *here* instead, and don't call the base method. } // Override OnMouseMove, etc. here as well. } 

However, you do not need the functionality that Panel adds to Control in general, that is, for its functioning as a container. Thus, in fact, you should inherit from Control if you do not need subcontrols.

Another improvement could only be Invalidate with a modified Rectangle . This will recolor one area and reduce the time of drawing. You can also pass srcRect to Graphics.DrawImage that srcRect calculated from e.ClipRectangle , for even better performance if the subclass Panel does not work for you.

+2
source

Usually just adding this.DoubleBuffered = true; Your code should do the trick.

If this is not the case, add this code to the form:

 protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x02000000; return cp; } } 

You can also change it using Reflection, but there is no advantage, neither readability, nor speed.

0
source

When drawing pixel maps in the Paint event, flickering is often caused by Windows shapes, as it first draws a background and then a pixel map. Thus, flicker is a background that becomes visible for a split second.

You can set the Opaque style in the ControlStyle panel property. This will turn the wallpaper, because Windows Forms now assumes that your code will fully display the contents of the panel.

0
source

Source: https://habr.com/ru/post/975231/


All Articles