C# for non C# programmer
-
Hi, I'm not fluent with C# (but i some how got forced into doing this). After form1 loads the user supplies information (Ip address, username ...etc), presses button 1, this connects them to a specialized video recorder. I then call the ExportImage() method to save the current frame to a bmp in a specified location. What i would like to do is automatically go to ExportImage() every 5 seconds. The only way i can get it to call ExportImage() again is to press the button. Any suggestions? Thanks Matt
static void Main()
{
Application.Run(new Form1());} private void button1\_Click(object sender, System.EventArgs e) { IDVRSystem system = SystemFactory.FindOrCreateSystem(cmbSystem.Text, 18772, txtUserID.Text, txtPassword.Text); system.Open(); if (system.ConnectionStatus == Integral.Client.Sources.ConnectionState.LoggedIn) { mRecorder = system.Recorders.FindByIPAddress(txtRecorder.Text); if (mRecorder != null) { // Sometimes a camera is not renderable because it is not 'detected.' // we'll just keep rendering until we get a successful render. foreach (IStream stream in mRecorder.Cameras) { mInfo = stream.Render(picVideo); if (mInfo != null) break; } //Console.WriteLine("button method"); //ExportImage(); } } } private void ExportImage() { Console.WriteLine("Export Image Method"); Bitmap bmp; double aspectRatio; System.Threading.Thread.Sleep(500); mInfo.VideoOverlay.GetCurrentImage(false, out bmp, out aspectRatio); Image img = bmp.GetThumbnailImage(800, 600, null, IntPtr.Zero); img.Save(@"C:\\Test.bmp"); img.Dispose(); bmp.Dispose(); }
-
Hi, I'm not fluent with C# (but i some how got forced into doing this). After form1 loads the user supplies information (Ip address, username ...etc), presses button 1, this connects them to a specialized video recorder. I then call the ExportImage() method to save the current frame to a bmp in a specified location. What i would like to do is automatically go to ExportImage() every 5 seconds. The only way i can get it to call ExportImage() again is to press the button. Any suggestions? Thanks Matt
static void Main()
{
Application.Run(new Form1());} private void button1\_Click(object sender, System.EventArgs e) { IDVRSystem system = SystemFactory.FindOrCreateSystem(cmbSystem.Text, 18772, txtUserID.Text, txtPassword.Text); system.Open(); if (system.ConnectionStatus == Integral.Client.Sources.ConnectionState.LoggedIn) { mRecorder = system.Recorders.FindByIPAddress(txtRecorder.Text); if (mRecorder != null) { // Sometimes a camera is not renderable because it is not 'detected.' // we'll just keep rendering until we get a successful render. foreach (IStream stream in mRecorder.Cameras) { mInfo = stream.Render(picVideo); if (mInfo != null) break; } //Console.WriteLine("button method"); //ExportImage(); } } } private void ExportImage() { Console.WriteLine("Export Image Method"); Bitmap bmp; double aspectRatio; System.Threading.Thread.Sleep(500); mInfo.VideoOverlay.GetCurrentImage(false, out bmp, out aspectRatio); Image img = bmp.GetThumbnailImage(800, 600, null, IntPtr.Zero); img.Save(@"C:\\Test.bmp"); img.Dispose(); bmp.Dispose(); }
matty2desmara wrote:
What i would like to do is automatically go to ExportImage() every 5 seconds.
Look up the different Timer classes in MSDN or Google. The System.Windows.Forms Timer class. Marc
-
matty2desmara wrote:
What i would like to do is automatically go to ExportImage() every 5 seconds.
Look up the different Timer classes in MSDN or Google. The System.Windows.Forms Timer class. Marc
Hi Marc, I forgot to include that in my code, but i was trying to use Thread.Sleep(time) with no success. I'm not sure where to put it. I think my main issue is getting exportImage() to be called more than once, since it's in the foreach loop i though it would be called on every scan, do you know why it's not ? the only way i can initiate a call again is to press button1. Thanks for your help Matt
-
Hi Marc, I forgot to include that in my code, but i was trying to use Thread.Sleep(time) with no success. I'm not sure where to put it. I think my main issue is getting exportImage() to be called more than once, since it's in the foreach loop i though it would be called on every scan, do you know why it's not ? the only way i can initiate a call again is to press button1. Thanks for your help Matt
You really don't want to use Thread.Sleep. You'll want to use the WinForm Timer class to set up a callback at a 5 second interval. The nice thing about the WinForm timer class is that you don't need to worry about marshalling to the application thread, since the event fires on the application thread. This means that you can update the UI, etc., without any worry about cross-threads. Marc