none
draw graphics in a timer RRS feed

  • Question

  • hi, i need to draw image on form1 paint but i need to write the code in a timer not in the form1paint method. can anyone helpme
    Wednesday, January 10, 2018 9:45 PM

All replies

  • Windows won't let you. You need to learn how Windows wants to do things.

    One thing that is important is, do you need to draw an entire image each time or do you only need to update an image?

    One way to do it is to create a new bitmap or update and existing bitmap and then put the bitmap into the form. Since the timer is a different thread you will need a way to get the bitmap from the timer's tick event handler to the form.

    Another way to do it is to transfer the relevant data from the timer's tick event handler to the form and then cause the paint event handler to redraw the image.

    The first choice has the advantage that all the processing is done in a thread separate from the UI so the UI is more likely to be responsive.



    Sam Hobbs
    SimpleSamples.Info

    Thursday, January 11, 2018 1:52 AM
  • Hi Yorgoo,

    Do you want to load many images from disk and play it by timer like animation?

    If so, hope the following code helps.

    Thanks and regards,

    E-John


        public partial class Frm_Main : Form
        {
            string strPath;
    	int idx = 1;
    
            public Frm_Main()
            {
                InitializeComponent();
            }
    
            private void Frm_Main_Load(object sender, EventArgs e)
            {
    	    // Get image path
    	    strPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = Image.FromFile(strPath + @"\image\1.jpg");
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                pictureBox1.Image = Image.FromFile(strPath + @"\image\" + idx + ".jpg");
    	    idx++;
    	    if (idx > 5)
    		idx = 1;
            }
        }

    Thursday, January 11, 2018 2:05 AM
  • Hello yorgoo,

    >>i need to draw image on form1 paint but i need to write the code in a timer not in the form1paint method. can anyone helpme

    If you want to use System.Windows.Forms.Timer to draw image on the form, the code should be like below.

    private void button1_Click(object sender, EventArgs e)
    		{
    			Timer myTimer = new Timer();
    
    			myTimer.Tick += new EventHandler(DrawImage);
    
    			// Sets the timer interval to 5 seconds.
    			myTimer.Interval = 5000;
    			myTimer.Start();
    		}
    
    		private void DrawImage(Object myObject,EventArgs myEventArgs) {
    	
    			Graphics myGraphics = this.CreateGraphics();
    
    			Bitmap bitmap = new Bitmap("1.1.jpg");
    
    			myGraphics.DrawImage(bitmap,0,0);
    
    			System.Threading.Thread.Sleep(500);
    
    			myGraphics.Clear(Color.AliceBlue);
    		}

    One important point is the timer events are raised on the UI thread.

    Or

    If you want to use System.Threading.Timer, the code should be like this.

    private void Test(object obj) {
    			Graphics myGraphics = this.CreateGraphics();
    
    			Bitmap bitmap = new Bitmap("1.1.jpg");
    
    			myGraphics.DrawImage(bitmap, 0, 0);
    
    			System.Threading.Thread.Sleep(500);
    
    			myGraphics.Clear(Color.AliceBlue);
    		}
    
    		private void button2_Click(object sender, EventArgs e)
    		{
    			System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(Test),null,0,5000);
    
    			
    		}

    Result

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 11, 2018 2:39 AM
    Moderator
  • Hello yorgoo,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and cooperation.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 1:02 PM
    Moderator