Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

C# for non C# programmer (2)

matty2desmaramatty2desmara Posts: 10Member
Hi, I'm not fluent in C# @ all. Basically what's happening here,is the user provides an IP address/DNS, username & password in form1. When button1 is click it connects to a specialized video recorder using the supplied library. When it's connected correctly ExportImage() is called and the method exports the current frame to the local drive. This works great, but the program does not call ExportImage() ever again, even though it's in a foreach loop, which i don't get.

I would like the program to call ExportImage() every 5 seconds (Exporting an image every 5 seconds). Right now the only way to get to get ExportImage() to be called again is to press button1.

Any Suggestions?


Much appreciated,
-Matt




[code]
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
using Integral.Client.Sources;
using System.Xml.Serialization;

namespace SimpleVideoOverlay
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtPassword;
private System.Windows.Forms.TextBox txtUserID;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox cmbSystem;
private System.Windows.Forms.PictureBox picVideo;
private System.Windows.Forms.Button button2;

private ArrayList RenderInfoArray;
private CompositeImageBuilder mBuilder = new CompositeImageBuilder();
private Bitmap mBitmap = new Bitmap(10, 10);
private IRecorder mRecorder;


private IStreamRenderInfo mInfo;

private TextBox txtRecorder;
private Label label4;
private CheckBox checkBox1;

///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;




public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}

///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}

if (mRecorder != null)
{
mRecorder = null;
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button1 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.txtPassword = new System.Windows.Forms.TextBox();
this.txtUserID = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.cmbSystem = new System.Windows.Forms.ComboBox();
this.picVideo = new System.Windows.Forms.PictureBox();
this.button2 = new System.Windows.Forms.Button();
this.txtRecorder = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
((System.ComponentModel.ISupportInitialize)(this.picVideo)).BeginInit();
this.SuspendLayout();
//
// openFileDialog1
//
this.openFileDialog1.Filter = "Integral Media Files (*.xpa,*.xpv)|*.xpa;*.xpv";
//
// button1
//
this.button1.Location = new System.Drawing.Point(328, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(55, 24);
this.button1.TabIndex = 15;
this.button1.Text = "Play";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label3
//
this.label3.Location = new System.Drawing.Point(2, 97);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(60, 15);
this.label3.TabIndex = 14;
this.label3.Text = "Password";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtPassword
//
this.txtPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtPassword.Location = new System.Drawing.Point(68, 97);
this.txtPassword.Name = "txtPassword";
this.txtPassword.PasswordChar = '*';
this.txtPassword.Size = new System.Drawing.Size(100, 20);
this.txtPassword.TabIndex = 13;
this.txtPassword.Text = "demo";
//
// txtUserID
//
this.txtUserID.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtUserID.Location = new System.Drawing.Point(68, 69);
this.txtUserID.Name = "txtUserID";
this.txtUserID.Size = new System.Drawing.Size(100, 20);
this.txtUserID.TabIndex = 11;
this.txtUserID.Text = "demo";
//
// label2
//
this.label2.Location = new System.Drawing.Point(12, 71);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(50, 15);
this.label2.TabIndex = 12;
this.label2.Text = "UserID";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 14);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(54, 16);
this.label1.TabIndex = 10;
this.label1.Text = "System";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// cmbSystem
//
this.cmbSystem.Items.AddRange(new object[] {
"199.71.141.50",
"12.11.242.229"});
this.cmbSystem.Location = new System.Drawing.Point(68, 13);
this.cmbSystem.Name = "cmbSystem";
this.cmbSystem.Size = new System.Drawing.Size(95, 21);
this.cmbSystem.TabIndex = 9;
this.cmbSystem.Text = "THHTest";
this.cmbSystem.SelectedIndexChanged += new System.EventHandler(this.cmbSystem_SelectedIndexChanged);
this.cmbSystem.TextChanged += new System.EventHandler(this.cmbSystem_TextChanged);
//
// picVideo
//
this.picVideo.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.picVideo.Location = new System.Drawing.Point(0, 123);
this.picVideo.Name = "picVideo";
this.picVideo.Size = new System.Drawing.Size(392, 321);
this.picVideo.TabIndex = 16;
this.picVideo.TabStop = false;
//
// button2
//
this.button2.Location = new System.Drawing.Point(328, 40);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 23);
this.button2.TabIndex = 17;
this.button2.Text = "OSD";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// txtRecorder
//
this.txtRecorder.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtRecorder.Location = new System.Drawing.Point(68, 43);
this.txtRecorder.Name = "txtRecorder";
this.txtRecorder.Size = new System.Drawing.Size(100, 20);
this.txtRecorder.TabIndex = 18;
//
// label4
//
this.label4.Location = new System.Drawing.Point(5, 48);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(57, 15);
this.label4.TabIndex = 19;
this.label4.Text = "Recorder";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(258, 71);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(80, 17);
this.checkBox1.TabIndex = 20;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.ClientSize = new System.Drawing.Size(392, 444);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.label4);
this.Controls.Add(this.txtRecorder);
this.Controls.Add(this.button2);
this.Controls.Add(this.picVideo);
this.Controls.Add(this.button1);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtPassword);
this.Controls.Add(this.txtUserID);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.cmbSystem);
this.Name = "Form1";
this.Text = "Simple OSD Sample";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.picVideo)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

///
/// The main entry point for the application.
///
[STAThread]




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)
{


foreach (IStream stream in mRecorder.Cameras)
{
mInfo = stream.Render(picVideo);


ExportImage();

if (mInfo != null)

break;
}

}
}
}





private void ExportImage()
{

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();


}



private void cmbSystem_TextChanged(object sender, EventArgs e)
{
txtRecorder.Text = cmbSystem.Text;
}

private void button2_Click(object sender, System.EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void cmbSystem_SelectedIndexChanged(object sender, EventArgs e)
{

}
}
}

[/code]

Comments

  • secutossecutos Posts: 10Member
    You could use a Timer control to raise an event at a specified time interval.


    [code]
    using System.Windows.Forms;

    Timer T = new Timer();
    T.Interval = 5000; // Raise event every 5 seconds
    T.Tick += new EventHandler(T_Tick); // Register the event handler


    void T_Tick(object sender, EventArgs e)
    {
    // Everything in here will run every 5 seconds
    ExportImage(); // Call your method
    }
    [/code]
Sign In or Register to comment.