0

I am not good at graphic plotting in C#. I am trying to do an animated plotting of a rectangular point on an Image in PictureBox. However, I am facing some flickering issues, I couldn't find the way; how to resolve this issue.

g = pictureBox1.CreateGraphics();
g.FillRectangle(Brushes.Green, Convert.ToInt32(x), Convert.ToInt32(y), 10, 10);

Thread.Sleep(20);
invalidate_pictureBox1();
update_pictureBox1();    

I have studied from other forums that this issue can be resolved using Timer instead of Thread sleep but don't know how to do it.

halfer
  • 19,824
  • 17
  • 99
  • 186
SN25
  • 69
  • 1
  • 12
  • I need urgently a coffee so your question has to wait for a moment. – rene Oct 08 '17 at 08:58
  • see [this](https://stackoverflow.com/questions/4305011/c-sharp-panel-for-drawing-graphics-and-scrolling) and go over https://stackoverflow.com/search?q=%5Bc%23%5D+%5Bwinforms%5D+timer+is%3Aq+hasaccepted%3Ayes – rene Oct 08 '17 at 09:02
  • 1
    Don't use `CreateGraphics`... Use the `PaintEventArgs.Graphics` property and do your drawing in the `OnPaint` event. Also, what is the intended purpose of the `Thread.Sleep`? – pinkfloydx33 Oct 08 '17 at 09:03
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Oct 08 '17 at 09:38
  • @pinkfloxydx33 I used thread sleep because I use this block of my code in a thread – SN25 Oct 08 '17 at 13:09
  • 1
    Thank you @halfer next I will be careful while next posting question.. – SN25 Oct 08 '17 at 13:11
  • You need to tell us more about the thread! From other than the UI thread you need to `Invoke` updates of controls! Note that anything drawn from a control.CreateGrapgics object is wiped out at each refresh ()aka non-prsistent' drawing; good for rubberbands, bad otherwise). Hence the flicker.. – TaW Oct 08 '17 at 15:29

1 Answers1

0

Draw what you want in Image of PictureBox, not on PictureBox control:

    private void timer1_Tick(object sender, EventArgs e)
    {
        Image img = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(img);
        g.FillRectangle(Brushes.Green, Convert.ToInt32(x), Convert.ToInt32(y), 10, 10);
        pictureBox1.Image = img;
    }

EDIT

if you want to draw on PictureBox control without flickering, put your drawing on pictureBox1.Paint event as follow:

    private void Form1_Load(object sender, EventArgs e)
    {
        // Your Solution
        int x = 0, y = 0;
        pictureBox1.Paint += new PaintEventHandler(delegate(object sender2, PaintEventArgs e2)
        {
            e2.Graphics.FillRectangle(Brushes.Green, x, y, 10, 10);
        });

        // Test
        buttonTest1.Click += new EventHandler(delegate(object sender2, EventArgs e2)
        {
            x++;
            pictureBox1.Invalidate();
        });

        buttonTest2.Click += new EventHandler(delegate(object sender2, EventArgs e2)
        {
            for (x = 0; x < pictureBox1.Width - 10; x++)
            {
                System.Threading.Thread.Sleep(50);
                pictureBox1.Invalidate();
                pictureBox1.Refresh();
            }
        });

        buttonTest3.Click += new EventHandler(delegate(object sender2, EventArgs e2)
        {
            System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
            t.Tick += new EventHandler(delegate(object sender3, EventArgs e3)
            {
                if (x <= pictureBox1.Width - 10)
                    x++;
                pictureBox1.Invalidate();
            });
            t.Enabled = true;
            t.Interval = 50;
        });
    }
Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
  • Thank you for your reply hussein, but I wonder at one point where should I call this timer event to put the delay in my program?? – SN25 Oct 08 '17 at 13:22
  • OP states he want to have an animation so drawng onto the control is the way to go. But only via the Paint event and its e.Graphics object. – TaW Oct 08 '17 at 15:29
  • The timer event is called repeatedly afer at least Interval ms as long as the Timer is active. To trigger the Paint event you invoke an Invalidate of the PBox.. – TaW Oct 08 '17 at 15:30
  • Thank you and excuse me if I did not get it right. Try edited version... – Hossein Golshani Oct 08 '17 at 22:15