0

To solve flickering problem in WinForms when drawing multiple shapes, I decided to use GraphicsPathdo draw all shapes and then render using Graphics. It works perfectly; drawing never flickers even if a high number of shapes are drawn.

 panel.Paint += (sender, args) => {
      var graphicsPath = new GraphicsPath(FillMode.Winding);

      for (int i = 0; i < 10; i++)
      {
          graphicsPath.AddEllipse(0, i * 5, 20, 20);
      }

      args.Graphics.FillPath(new SolidBrush(Color.Red),  graphicsPath);

However, in this case all ellipses are the same color. Drawing every ellipse using graphics.FillPath() also causes flickering when shapes are redrawn (fot instance on Paint event).

Is there a way to draw each shape with a different color while continuing bulk drawing such as above one?

ozgur
  • 2,549
  • 4
  • 25
  • 40
  • `form.CreateGraphics();` Usually wrong. Use the `Paint` event and its `e.Graphics` object to draw persistent graphics.. Also: Do set `Form.DoubleBuffered = true`to prevent flicker.! Then maybe your code will work, but as we see only so little it is hard to tell. Do fix these issues first! - Of course __one__ `GraphicsPath` can only have __one__ color or maybe several if you fill it with e.g. a `LinearGradientBrush`.. – TaW Nov 17 '18 at 13:43
  • @TaW I am actually doing that. I just forgot when I was trying to simplify the example. Thank you. – ozgur Nov 17 '18 at 13:49
  • OK, that looks different. But what is the question? One Path, one Brush, no way around this. And no need with DoubleDuffering no flicker will happen. What control do you draw on? – TaW Nov 17 '18 at 13:55
  • **And no need with DoubleDuffering no flicker will happen** what do you mean by that? – ozgur Nov 17 '18 at 13:57
  • `DoubleBuffered` is a property some Controls expose (Form) or have already on (PictureBox). Most others can be tweaked to turn it on too. See [here](https://stackoverflow.com/questions/44185298/update-datagridview-very-frequently/44188565#44188565) for an example code.. – TaW Nov 17 '18 at 13:59
  • [Using Nested Graphics Containers](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-nested-graphics-containers). You can also manage a `List`. But it would be better to have specialized classed (possibly derived from a common abstract class/interface) that describe each shape, which can then be added to a GraphicsPath. The Double Buffering feature is relevant here. – Jimi Nov 17 '18 at 14:00
  • @Jimi: While very interesting I don't see how that link is relevant here. He wants to break up the path elements he had only lumped together to avoid flicker which only happend because he didn't know about buffering.. – TaW Nov 17 '18 at 14:02
  • @TaW I pushed the Enter key before completing the comment :) – Jimi Nov 17 '18 at 14:03
  • hehe, my fault I read too fast ;-) – TaW Nov 17 '18 at 14:03

0 Answers0