PageRenderTime 3ms CodeModel.GetById 18ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/CmsCheckin/TakePicture.cs

https://bitbucket.org/mahalowe/bvcms
C# | 112 lines | 99 code | 12 blank | 1 comment | 8 complexity | 29a3e829a65b0ba0ee2fe10a7938fc19 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Linq;
  7using System.Text;
  8using System.Windows.Forms;
  9using System.Net;
 10using System.Xml.Linq;
 11using System.Drawing.Printing;
 12using System.Xml.Serialization;
 13using System.IO;
 14using System.Drawing.Imaging;
 15using System.Runtime.InteropServices;
 16using System.Collections.Specialized;
 17
 18namespace CmsCheckin
 19{
 20    public partial class TakePicture : Form
 21    {
 22        private Capture cam;
 23        IntPtr m_ip = IntPtr.Zero;
 24        const int VIDEODEVICE = 0; // zero based index of video capture device to use
 25        const int VIDEOWIDTH = 640; // Depends on video device caps
 26        const int VIDEOHEIGHT = 480; // Depends on video device caps
 27        const int VIDEOBITSPERPIXEL = 64; // BitsPerPixel values determined by device
 28
 29        public TakePicture()
 30        {
 31            InitializeComponent();
 32
 33            try
 34            {
 35                cam = new Capture(VIDEODEVICE, VIDEOWIDTH, VIDEOHEIGHT, VIDEOBITSPERPIXEL, imageResizer1.pctCamera);
 36                imageResizer1.btnTakePicture.Click += new EventHandler(btnTakePicture_Click);
 37                imageResizer1.btnSave.Click += new EventHandler(btnUploadPicture_Click);
 38            }
 39            catch (Exception)
 40            {
 41                //Handle exception
 42            }
 43        }
 44        private void btnTakePicture_Click(object sender, EventArgs e)
 45        {
 46            const string STR_ShowCamera = "Show Camera";
 47            if (imageResizer1.btnTakePicture.Text == STR_ShowCamera)
 48            {
 49                imageResizer1.pctCamera.Visible = true;
 50                imageResizer1.btnTakePicture.Text = "Take Picture";
 51                return;
 52            }
 53            if (cam != null)
 54            {
 55                Cursor.Current = Cursors.WaitCursor;
 56
 57                if (m_ip != IntPtr.Zero)
 58                {
 59                    Marshal.FreeCoTaskMem(m_ip);
 60                    m_ip = IntPtr.Zero;
 61                }
 62
 63                m_ip = cam.Click();
 64                Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip);
 65                b.RotateFlip(RotateFlipType.RotateNoneFlipY);
 66
 67                imageResizer1.BaseImage = b;
 68                imageResizer1.pctCamera.Visible = false;
 69                imageResizer1.btnTakePicture.Text = STR_ShowCamera;
 70
 71                Cursor.Current = Cursors.Default;
 72            }
 73
 74        }
 75        private static byte[] ConvertImageToByteArray(Image image, ImageFormat format)
 76        {
 77            byte[] b;
 78            try
 79            {
 80
 81                using (var ms = new MemoryStream())
 82                {
 83                    image.Save(ms, format);
 84                    b = ms.ToArray();
 85                }
 86            }
 87            catch (Exception) { throw; }
 88            return b;
 89        }
 90
 91        private void btnUploadPicture_Click(object sender, EventArgs e)
 92        {
 93            this.Cursor = Cursors.WaitCursor;
 94            var bits = ConvertImageToByteArray(imageResizer1.SaveImage(), ImageFormat.Jpeg);
 95            var url = new Uri(new Uri(Program.URL), "Checkin2/UploadImage/" + Program.PeopleId);
 96            var wc = Util.CreateWebClient();
 97            wc.UploadData(url, "POST", bits);
 98            this.Close();
 99        }
100
101        private void TakePicture_FormClosing(object sender, FormClosingEventArgs e)
102        {
103            if (cam != null)
104                cam.Dispose();
105        }
106
107        private void Return_Click(object sender, EventArgs e)
108        {
109            Close();
110        }
111    }
112}