public TakePicture(MainForm mainForm) { _mainForm = mainForm; InitializeComponent(); _saveScreenImage = false; _texturedBrushLocation = Point.Empty; _faceTexture = null; _mainForm.ConfigScreen = ConfigScreenEnum.DoNotDraw; _webCam = new WebCam(mainForm); _webCam.SetViewControl(this); _streamWebCam = true; _faceColor = Color.Green; _penBlack = new Pen(Color.Black, 3f); _penCrosshairs = new Pen(Color.SkyBlue, 3f); _brushWhite = new SolidBrush(Color.White); txtScore.Text = _mainForm.Score.ToString(); chkSolidBrush.Checked = true; UpdateUI(); txtScore.TextChanged += new System.EventHandler(txtScore_TextChanged); // Computed screen coordinates. Debug.WriteLine("btnHead location: {0}, {1}", ((this.Width - btnHead.Width) / 2).ToString(), btnHead.Location.Y.ToString()); Debug.WriteLine("btnStartWebCam location: {0}, {1}", ((this.Width - btnStartWebCam.Width) / 2).ToString(), btnStartWebCam.Location.Y.ToString()); // Set scrollbar ranges. Helper_SetScrollbarLimits(vScroll_HaircutTool, _mainForm._settings.Haircut_Offset); Helper_SetScrollbarLimits(vScroll_HeadHeight, _mainForm._settings.Face_Height); Helper_SetScrollbarLimits(hScroll_HeadWidth, _mainForm._settings.Face_Width); Helper_SetScrollbarLimits(vScroll_ImageAdjust, _mainForm._settings.HeadImage_VertOffset); Helper_SetScrollbarLimits(hScroll_ImageAdjust, _mainForm._settings.HeadImage_HorzOffset); }
/// <summary> /// Constructor. /// </summary> /// <param name="mainForm"></param> public Config(MainForm mainForm) { _mainForm = mainForm; InitializeComponent(); chkFlipImageHorizontally.Checked = _mainForm._settings.FlipImageHorizontally; _webCam = new WebCam(mainForm); _webCam.SetViewControl(pnlWebCam); // Call event handlers. Used for validation for current values. radioFullScaled_CheckedChanged(null, new EventArgs()); txtName_TextChanged(null, new EventArgs()); txtScore_TextChanged(null, new EventArgs()); // Create folder and image path name. Then send it to image loader. _bitmapFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _bitmapFilename); SetImage(_bitmapFilename); // Initialize service client instance. ScoreServiceClient item = new ScoreKeeper.ScoreServiceClient(); lblUrl.Text = item.Endpoint.ListenUri.ToString(); }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.Clear(Color.White); e.Graphics.SmoothingMode = SmoothingMode.HighQuality; e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear; // Store last WebCam captured frame in case stream paused. if (_webCam.LatestFrame != null && _streamWebCam) { _bufferedLastFrame = _webCam.LatestFrame; // Reset all scroll bars. hScroll_ImageAdjust.Value = 0; vScroll_ImageAdjust.Value = 0; } // Manipulate WebCam frame. if (_bufferedLastFrame != null) { Brush defaultBrush = new SolidBrush(_faceColor); // Set default brush. if (!chkSolidBrush.Checked && _faceTexture != null) { // Bitmap tiling image. defaultBrush = new TextureBrush(_faceTexture); } // Calculate master clip region to prevent images spilling out of face region. // We will use the entire head ellipse. Rectangle masterClippingRect = new Rectangle(pnlHeadOutline.Location, pnlHeadOutline.Size); GraphicsPath masterClippingPath = new GraphicsPath(); masterClippingPath.AddEllipse(masterClippingRect); Region masterClippingRegion = new Region(masterClippingPath); // Master clipping region set. e.Graphics.Clip = masterClippingRegion; // Get bitmap of center portion of webcam. Rectangle cropRegion = new Rectangle( new Point( (int)(((float)_bufferedLastFrame.Width) * (1.0f - _mainForm._settings.CropWebCam_WidthPercent) / 2), (int)((float)_bufferedLastFrame.Height * (1.0f - _mainForm._settings.CropWebCam_HeightPercent) / 2) ), new Size( (int)(((float)_bufferedLastFrame.Width) * _mainForm._settings.CropWebCam_WidthPercent), (int)((float)_bufferedLastFrame.Height * _mainForm._settings.CropWebCam_HeightPercent) ) ); // Adjust webcam image by user scrollbars. cropRegion.Offset(hScroll_ImageAdjust.Value * -1, vScroll_ImageAdjust.Value); Bitmap bm = WebCam.Crop(_bufferedLastFrame, cropRegion); bm = WebCam.Resize(bm, pnlHeadOutline.Size); if (chkInvertFaceColors.Checked) { Color c; for (int i = 0; i < bm.Width; i++) { for (int j = 0; j < bm.Height; j++) { c = bm.GetPixel(i, j); bm.SetPixel(i, j, Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B)); } } } int offset = (pnlHeadOutline.Width - bm.Width) / 2; Rectangle webCamImageRect = new Rectangle(pnlHeadOutline.Location.X + offset, pnlHeadOutline.Location.Y, bm.Width, bm.Height); // Draw camera image. e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; e.Graphics.DrawImage(bm, webCamImageRect); // Draw vertical alignment crosshairs. if (_streamWebCam && chkCrosshairs.Checked) { e.Graphics.DrawLine(_penCrosshairs, this.Width / 2, 0, this.Width / 2, this.Height); } // Add ears. e.Graphics.ResetClip(); Rectangle rightEar = new Rectangle(pnlRightEar.Location, pnlRightEar.Size); e.Graphics.FillEllipse(defaultBrush, rightEar); e.Graphics.DrawEllipse(_penBlack, rightEar); Rectangle leftEar = new Rectangle(pnlLeftEar.Location, pnlLeftEar.Size); e.Graphics.FillEllipse(defaultBrush, leftEar); e.Graphics.DrawEllipse(_penBlack, leftEar); // Draw eye line out of bound crosshair. e.Graphics.Clip = masterClippingRegion; if (_streamWebCam && chkCrosshairs.Checked) { e.Graphics.DrawLine(_penCrosshairs, leftEar.Location, rightEar.Location); } // Create inner face ellipse and graphis path. Rectangle innerEllipse = new Rectangle(btnHead.Location, btnHead.Size); innerEllipse.Inflate(hScroll_HeadWidth.Value, vScroll_HeadHeight.Value); GraphicsPath innerPath = new GraphicsPath(); innerPath.AddEllipse(innerEllipse); // Create haircut ellipse and graphis path. // Zero offset coverage is 20% down the face. Rectangle haircutRect = masterClippingRect; haircutRect.Height = haircutRect.Height / 5 + vScroll_HaircutTool.Value; GraphicsPath pathHaircut = new GraphicsPath(); pathHaircut.AddRectangle(haircutRect); // Create drawing region from face and haircut pieces. Region head = new Region(masterClippingPath); head.Exclude(innerPath); head.Union(pathHaircut); e.Graphics.FillRegion(defaultBrush, head); // Outline head ellipse. e.Graphics.ResetClip(); e.Graphics.DrawEllipse(_penBlack, masterClippingRect); // Draw neck. int neckEllipseWidth = 30; e.Graphics.FillRectangle(defaultBrush, new Rectangle(pnlNeck.Location, pnlNeck.Size)); Rectangle leftSideNeck = new Rectangle(pnlNeck.Location, pnlNeck.Size); leftSideNeck.Width = neckEllipseWidth; leftSideNeck.X = leftSideNeck.X - leftSideNeck.Width / 2; e.Graphics.FillEllipse(_brushWhite, leftSideNeck); e.Graphics.DrawArc(_penBlack, leftSideNeck, -90f, 180f); Point leftShoulder = new Point(leftSideNeck.X + neckEllipseWidth / 2, leftSideNeck.Y + leftSideNeck.Height); Rectangle rightSideNeck = new Rectangle(pnlNeck.Location, pnlNeck.Size); rightSideNeck.Width = neckEllipseWidth; rightSideNeck.X = rightSideNeck.X + pnlNeck.Size.Width - rightSideNeck.Width / 2; e.Graphics.FillEllipse(_brushWhite, rightSideNeck); e.Graphics.DrawArc(_penBlack, rightSideNeck, 90f, 180f); Point rightShoulder = new Point(rightSideNeck.X + neckEllipseWidth / 2, rightSideNeck.Y + rightSideNeck.Height); e.Graphics.DrawLine(_penBlack, leftShoulder, rightShoulder); // Capture image from screen and write it to file. Bitmap memoryImage = new Bitmap(pnlBitmapImage.Width, pnlBitmapImage.Height, e.Graphics); Graphics memoryGraphics = Graphics.FromImage(memoryImage); Point screenCoords = this.PointToScreen(pnlBitmapImage.Location); // Image from screen is "memoryImage" memoryGraphics.CopyFromScreen(screenCoords.X, screenCoords.Y, 0, 0, pnlBitmapImage.Size); if (_saveScreenImage) { memoryImage.Save("LastestImage.png", ImageFormat.Png); _saveScreenImage = false; SendToServer(memoryImage, _mainForm.Score, txtName.Text); } // See if need to get texture for brush. if (_texturedBrushLocation != Point.Empty) { // Capture image from screen and write it to file. int textureWidth = 6; memoryImage = new Bitmap(textureWidth, textureWidth, e.Graphics); memoryGraphics = Graphics.FromImage(memoryImage); screenCoords = this.PointToScreen(_texturedBrushLocation); memoryGraphics.CopyFromScreen(screenCoords.X, screenCoords.Y, 0, 0, pnlBitmapImage.Size); _faceTexture = (Bitmap)memoryImage.Clone(); // Reset location to prevent continuous updating. _texturedBrushLocation = Point.Empty; } } }