private void On_TakeImageButtonClick(object sender, EventArgs e) { if (webCam != null) { if (stillImageBox.Image != null) { stillImageBox.Image.Dispose(); } if (originalPicture != null) { originalPicture.Dispose(); } try { System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; // Release any previous buffer if (ip != IntPtr.Zero) { Marshal.FreeCoTaskMem(ip); ip = IntPtr.Zero; } // capture image ip = webCam.Click(); originalPicture = new System.Drawing.Bitmap(webCam.Width, webCam.Height, webCam.Stride, PixelFormat.Format24bppRgb, ip); originalPicture.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY); System.Drawing.Bitmap result = new System.Drawing.Bitmap(200, 150); using (System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(result)) { graph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; graph.DrawImage(originalPicture, 0, 0, 200, 150); System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Purple, 1); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { graph.DrawRectangle(pen, 25 + 25 - 10 + 50 * j, 25 - 10 + 50 * i, 20, 20); } } pen.Dispose(); } stillImageBox.Image = result; System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default; } catch { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(VIDEOWIDTH, VIDEOHEIGHT); using (System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bitmap)) { System.Drawing.Rectangle size = new System.Drawing.Rectangle(0, 0, VIDEOWIDTH, VIDEOHEIGHT); graph.FillRectangle(System.Drawing.Brushes.White, size); } stillImageBox.Image = bitmap; webCam.Dispose(); webCam = null; } } }
private void On_TakeImageButtonClick(object sender, EventArgs e) { if (webCam != null) { if (stillImageBox.Image != null) { stillImageBox.Image.Dispose(); } try { System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; // Release any previous buffer if (ip != IntPtr.Zero) { Marshal.FreeCoTaskMem(ip); ip = IntPtr.Zero; } // capture image ip = webCam.Click(); System.Drawing.Bitmap b = new System.Drawing.Bitmap(webCam.Width, webCam.Height, webCam.Stride, PixelFormat.Format24bppRgb, ip); b.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY); readedColors = rubiksColorReader.ReadRubiksSide(b, new Vector2((webCam.Width - webCam.Height) / 2 + webCam.Height / 6, webCam.Height / 6), webCam.Height / 3, webCam.Height / 12); System.Drawing.Bitmap res = new System.Drawing.Bitmap(VIDEOWIDTH, VIDEOHEIGHT); using (System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(res)) { System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Purple, 1); graph.DrawImage(b, 0, 0, 320, 240); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { graph.DrawRectangle(pen, 40 + 30 + 80 * j, 30 + 80 * i, 20, 20); } } pen.Dispose(); } b.Dispose(); stillImageBox.Image = res; System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default; } catch { readedColors = new Color[3, 3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { readedColors[i, j] = Color.White; } } System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(VIDEOWIDTH, VIDEOHEIGHT); using (System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bitmap)) { System.Drawing.Rectangle size = new System.Drawing.Rectangle(0, 0, VIDEOWIDTH, VIDEOHEIGHT); graph.FillRectangle(System.Drawing.Brushes.White, size); } stillImageBox.Image = bitmap; webCam.Dispose(); webCam = null; UpdateCameraDevices(); } finally { int k = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { rubiksReadedColors.SetColor(k, readedColors[j, i]); k++; } } } } }