/CmsCheckin/TakePicture.cs
C# | 112 lines | 99 code | 12 blank | 1 comment | 8 complexity | 29a3e829a65b0ba0ee2fe10a7938fc19 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-3.0
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.Xml.Linq;
- using System.Drawing.Printing;
- using System.Xml.Serialization;
- using System.IO;
- using System.Drawing.Imaging;
- using System.Runtime.InteropServices;
- using System.Collections.Specialized;
-
- namespace CmsCheckin
- {
- public partial class TakePicture : Form
- {
- private Capture cam;
- IntPtr m_ip = IntPtr.Zero;
- const int VIDEODEVICE = 0; // zero based index of video capture device to use
- const int VIDEOWIDTH = 640; // Depends on video device caps
- const int VIDEOHEIGHT = 480; // Depends on video device caps
- const int VIDEOBITSPERPIXEL = 64; // BitsPerPixel values determined by device
-
- public TakePicture()
- {
- InitializeComponent();
-
- try
- {
- cam = new Capture(VIDEODEVICE, VIDEOWIDTH, VIDEOHEIGHT, VIDEOBITSPERPIXEL, imageResizer1.pctCamera);
- imageResizer1.btnTakePicture.Click += new EventHandler(btnTakePicture_Click);
- imageResizer1.btnSave.Click += new EventHandler(btnUploadPicture_Click);
- }
- catch (Exception)
- {
- //Handle exception
- }
- }
- private void btnTakePicture_Click(object sender, EventArgs e)
- {
- const string STR_ShowCamera = "Show Camera";
- if (imageResizer1.btnTakePicture.Text == STR_ShowCamera)
- {
- imageResizer1.pctCamera.Visible = true;
- imageResizer1.btnTakePicture.Text = "Take Picture";
- return;
- }
- if (cam != null)
- {
- Cursor.Current = Cursors.WaitCursor;
-
- if (m_ip != IntPtr.Zero)
- {
- Marshal.FreeCoTaskMem(m_ip);
- m_ip = IntPtr.Zero;
- }
-
- m_ip = cam.Click();
- Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip);
- b.RotateFlip(RotateFlipType.RotateNoneFlipY);
-
- imageResizer1.BaseImage = b;
- imageResizer1.pctCamera.Visible = false;
- imageResizer1.btnTakePicture.Text = STR_ShowCamera;
-
- Cursor.Current = Cursors.Default;
- }
-
- }
- private static byte[] ConvertImageToByteArray(Image image, ImageFormat format)
- {
- byte[] b;
- try
- {
-
- using (var ms = new MemoryStream())
- {
- image.Save(ms, format);
- b = ms.ToArray();
- }
- }
- catch (Exception) { throw; }
- return b;
- }
-
- private void btnUploadPicture_Click(object sender, EventArgs e)
- {
- this.Cursor = Cursors.WaitCursor;
- var bits = ConvertImageToByteArray(imageResizer1.SaveImage(), ImageFormat.Jpeg);
- var url = new Uri(new Uri(Program.URL), "Checkin2/UploadImage/" + Program.PeopleId);
- var wc = Util.CreateWebClient();
- wc.UploadData(url, "POST", bits);
- this.Close();
- }
-
- private void TakePicture_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (cam != null)
- cam.Dispose();
- }
-
- private void Return_Click(object sender, EventArgs e)
- {
- Close();
- }
- }
- }