Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Camera Vision - video surveillance on C#

, 9 Oct 2006 GPL3
A C# video surveillance application, which allows monitoring several IP cameras simultaneously.
cv_demo.zip
axis.dll
cv.exe
dlink.dll
dshow.dll
jpeg.dll
local.dll
mjpeg.dll
multisource.dll
panasonic.dll
pixord.dll
stardot.dll
stream.dll
videosource.dll
cv_src.zip
axis
axis.csproj.user
cv
cv.csproj.user
bin
Debug
Release
Controls
Forms
Tools
dlink
dlink.csproj.user
dshow
dshow.csproj.user
Core
icons
about.png
fit_to_size.png
folder-opened.png
folder.png
table.png
text_braille.png
videocamera.ico
videocamera_stop.png
window_sidebar.png
jpeg
jpeg.csproj.user
local
local.csproj.user
mjpeg
mjpeg.csproj.user
multisource
multisource.csproj.user
panasonic
panasonic.csproj.user
pixord
pixord.csproj.user
stardot
stardot.csproj.user
stream
stream.csproj.user
videosource
videosource.csproj.user
// Camera Vision
//
// Copyright � Andrew Kirillov, 2005-2006
// andrew.kirillov@gmail.com
//
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Threading;

namespace CameraViewer
{
	/// <summary>
	/// Summary description for CameraWindow.
	/// </summary>
	public class CameraWindow : System.Windows.Forms.Control
	{
		private Camera	camera = null;
		private bool	autosize = false;
		private bool	needSizeUpdate = false;
		private bool	firstFrame = true;

		// AutoSize property
		[DefaultValue(false)]
		public bool AutoSize
		{
			get { return autosize; }
			set
			{
				autosize = value;
				UpdatePosition();
			}
		}

		// Camera property
		[Browsable(false)]
		public Camera Camera
		{
			get { return camera; }
			set
			{
				// lock
				Monitor.Enter(this);

				// detach event
				if (camera != null)
				{
					camera.NewFrame -= new EventHandler(camera_NewFrame);
				}

				camera = value;
				needSizeUpdate = true;
				firstFrame = true;

				// atach event
				if (camera != null)
				{
					camera.NewFrame += new EventHandler(camera_NewFrame);
				}

				// unlock
				Monitor.Exit(this);
			}
		}

		// Constructor
		public CameraWindow()
		{
			SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer |
				ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
		}

		// Paint control
		protected override void OnPaint(PaintEventArgs pe)
		{
			if ((needSizeUpdate) || (firstFrame))
			{
				UpdatePosition();
				needSizeUpdate = false;
			}

			// lock
			Monitor.Enter(this);

			Graphics	g = pe.Graphics;
			Rectangle	rc = this.ClientRectangle;
			Pen			pen = new Pen(Color.Black, 1);

			// draw rectangle
			g.DrawRectangle(pen, rc.X, rc.Y, rc.Width - 1, rc.Height - 1);

			if (camera != null)
			{
				camera.Lock();

				// draw frame
				if (camera.LastFrame != null)
				{
					g.DrawImage(camera.LastFrame, rc.X + 1, rc.Y + 1, rc.Width - 2, rc.Height - 2);
					firstFrame = false;
				}
				else
				{
					// Create font and brush
					Font drawFont = new Font("Arial", 12);
					SolidBrush drawBrush = new SolidBrush(Color.White);

					g.DrawString("Connecting ...", drawFont, drawBrush, new PointF(5, 5));

					drawBrush.Dispose();
					drawFont.Dispose();
				}

				camera.Unlock();
			}

			pen.Dispose();

			// unlock
			Monitor.Exit(this);

			base.OnPaint(pe);
		}

		// update position and size of the control
		public void UpdatePosition()
		{
			// lock
			Monitor.Enter(this);

			if (autosize)
			{
				Rectangle	rc = this.Parent.ClientRectangle;
				int			width = 320;
				int			height = 240;

				if (camera != null)
				{
					camera.Lock();

					// get frame dimension
					if (camera.LastFrame != null)
					{
						width = camera.LastFrame.Width;
						height = camera.LastFrame.Height;
					}
					camera.Unlock();
				}

				//
				this.SuspendLayout();
				this.Location = new Point((rc.Width - width - 2) / 2, (rc.Height - height - 2) / 2);
				this.Size = new Size(width + 2, height + 2);
				this.ResumeLayout();

			}
			// unlock
			Monitor.Exit(this);
		}

		// On new frame ready
		private void camera_NewFrame(object sender, System.EventArgs e)
		{
			Invalidate();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Andrew Kirillov
Software Developer (Senior) Cisco Systems
United Kingdom United Kingdom
Started software development at about 15 years old and it seems like now it lasts most part of my life. Fortunately did not spend too much time with Z80 and BK0010 and switched to 8086 and further. Similar with programming languages – luckily managed to get away from BASIC and Pascal to things like Assembler, C, C++ and then C#. Apart from daily programming for food, do it also for hobby, where mostly enjoy areas like Computer Vision, Robotics and AI. This led to some open source stuff like AForge.NET.
 
Going out of computers I am just a man loving his family, enjoying traveling, a bit of books, a bit of movies and a mixture of everything else. Always wanted to learn playing guitar, but it seems like 6 strings are much harder than few dozens of keyboard’s keys. Will keep progressing ...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 10 Oct 2006
Article Copyright 2006 by Andrew Kirillov
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid