Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GDI+
Hi,
i'm trying to draw a filled ellipse on the screen according to a sensor signal which is read in via RS232. Currently I use a ReceivedData Handler to call my draw-method in which i first clear the form and then draw the ellipse according to the current signal value. This results in exessive flickering.
From reading through posts concerning flicker effects, I'm not sure if this way is how I should do it.
Any suggestions of how this should be done?
 
Thanks a lot in advance!
 
Michael
Posted 13-Dec-11 4:20am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Don't do the clear!
Instead of drawing on your form, create a simple Panel class:
public class MyPanel : Panel
    {
    protected override void OnPaintBackground(PaintEventArgs e)
        {
        // Prevent background clearing
        }
    }
The only purpose of this is to disable background clearing.
Add the MyPanel to your form.
Now draw your ellipse on the panel, in the Paint event.
What I would do is to create a region which is the elipse:
            GraphicsPath path = new GraphicsPath();
            Rectangle rect = new Rectangle(0, 0, 100, 100);
            path.AddEllipse(rect);
            Region myClearBit = new Region();
            myClearBit.MakeEmpty();
            myClearBit.Union(path);
            e.Graphics.ExcludeClip(myClearBit);
            e.Graphics.FillRectangle(Brushes.Green,e.Graphics.ClipBounds);
            e.Graphics.SetClip(myClearBit, CombineMode.Replace);
            e.Graphics.FillPath(Brushes.Red, path);
This will draw a red circle on a green background, and shouldn't flicker as it re-sizes.
  Permalink  
Comments
SAKryukov at 13-Dec-11 11:24am
   
Griff, this is important information, but as the solution of the problem in question is not 100% accurate. This technique is neither always required nor it is sufficient and all cases.
 
Please see my solution.
--SA
Member 8404471 at 14-Dec-11 4:05am
   
Thank you so much, you saved me a lot of time! I'm using your solution painting on a form instead of a panel. but it works like charm!
OriginalGriff at 14-Dec-11 4:34am
   
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The advice by Griff may be not enough to avoid flicker in all situations; and this technique is not always needed.
 
In many or even in most cases you need to setup double buffering. Consider you have a control class. You really need to create a class as you need the method System.Windows.Forms.Control.SetStyle, which is protected, see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx[^].
 
You will need to add the styles System.Windows.Forms.ControlStyles.AllPaintingInWmPaint.OptimizedDoubleBuffer | System.Windows.Forms.ControlStyles., see http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx[^].
 
However, if your control is Form, you can simply its property DoubleBuffered, see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx[^].
 
—SA
  Permalink  
Comments
Member 8404471 at 14-Dec-11 4:04am
   
Thanks a lot, but I tried it now with and without using the DoubleBuffer Option, but it doesn't make a difference in my case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 230
1 OriginalGriff 202
2 Richard MacCutchan 185
3 Tomas Takac 146
4 CPallini 125
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 13 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100