Thread: Timer update image

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Åland, Finland
    Posts
    11

    Timer update image

    Hello
    Over to the next problem; how do i update the image one time per second?

    The timer is working, but when i complie it says to me that i have to use EventHandler() instead of directly MainBox.Refresh(), so how do i do one void/subclass or something that i could use "MainBox.Refresh()" in?

    Thanks a lot for the previous answers!

    Code:
    #region using
    
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.IO;
    using System.Net;
    
    #endregion
    
    public class MainsClass
    {
    	
    	public static void Main()
    	{
    		
    		new InterfaceClass();
    		
    	}
    	
    }
    
    public class InterfaceClass
    {
    	public InterfaceClass()
    	{
    		Form MainWindow = new Form();
    		PictureBox MainBox = new PictureBox();
    				
    		MainWindow.Text = "Segel Kamera";
    		MainWindow.StartPosition = FormStartPosition.CenterScreen;
    		MainWindow.Width = 640;
    		MainWindow.Height = 480;
    		
    		WebRequest reg = WebRequest.Create("http://raceoffice.segel.aland.fi:81/record/current.jpg");
    		Stream stream = reg.GetResponse().GetResponseStream();
    		
    		Bitmap img = new Bitmap(stream);
    		
    		MainBox.BackgroundImage = img;
    		MainBox.Width = 640;
    		MainBox.Height = 480;
    		MainBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
    		
    		Timer MainTimer = new Timer();
    		MainTimer.Interval = 1000;
    		MainTimer.Start();
    		MainTimer.Tick += MainBox.Refresh();
    		
    		MainWindow.Controls.Add(MainBox);
    		MainWindow.ShowDialog();
    	}
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Here's one way. I've used a Thread rather than a Timer:

    Code:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.IO;
    using System.Net;
    using System.Threading;
    
    public class MainsClass
    {
    	
    	public static void Main()
    	{
    		
    		new InterfaceClass();
    		
    	}
    	
    }
    
    public class InterfaceClass
    {
    	private Thread thread;
    	private PictureBox MainBox;
    	private delegate void CrossThreadOperationDelegate();
    
    	public InterfaceClass()
    	{
    		Form MainWindow = new Form();
    		MainBox = new PictureBox();
    				
    		MainWindow.Text = "Segel Kamera";
    		MainWindow.StartPosition = FormStartPosition.CenterScreen;
    		MainWindow.Width = 640;
    		MainWindow.Height = 480;
    		
    		MainBox.Width = 640;
    		MainBox.Height = 480;
    		MainBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
    		
    		MainWindow.Controls.Add(MainBox);
    		MainWindow.ShowDialog();
    
    		thread = new Thread(new ThreadStart(Worker));
    		thread.Start();
    	}
    
    	private void Worker()
    	{
    		while (true)
    		{
    			Thread.Sleep(1000);
    			UpdatePicture();
    		}
    	}
    
    	private void UpdatePicture()
    	{
    		if (MainBox.InvokeRequired)
    		{
    			MainBox.BeginInvoke(new CrossThreadOperationDelegate(UpdatePicture));
    		}
    		else
    		{
    			WebRequest reg = WebRequest.Create("http://raceoffice.segel.aland.fi:81/record/current.jpg");
    			Stream stream = reg.GetResponse().GetResponseStream();
    			Bitmap img = new Bitmap(stream);
    			MainBox.BackgroundImage = img;
    		}
    	}
    }

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    Åland, Finland
    Posts
    11
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  3. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  4. Replies: 4
    Last Post: 03-02-2003, 09:12 AM