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