private void ShowImageCapture(object sender, EventArgs e) { ApplicationSettings settings = new ApplicationSettings().GetOrCreateApplicationSettings(); var minimizePreference = settings.ClientSettings.MinimizeToTray; if (minimizePreference) { settings.ClientSettings.MinimizeToTray = false; settings.Save(settings); } HideAllForms(); var userAcceptance = MessageBox.Show("The image capture process will now begin and display a screenshot of the" + " current desktop in a custom full-screen window. You may stop the capture process at any time by pressing" + " the 'ESC' key, or selecting 'Close' at the top left. Simply create the image by clicking once to start" + " the rectangle and clicking again to finish. The image will be cropped to the boundary within the red rectangle." + " Shall we proceed?", "Image Capture", MessageBoxButtons.YesNo); if (userAcceptance == DialogResult.Yes) { frmImageCapture imageCaptureForm = new frmImageCapture(); if (imageCaptureForm.ShowDialog() == DialogResult.OK) { CommandItemControl inputBox = (CommandItemControl)sender; UIPictureBox targetPictureBox = (UIPictureBox)inputBox.Tag; targetPictureBox.Image = imageCaptureForm.UserSelectedBitmap; var convertedImage = Common.ImageToBase64(imageCaptureForm.UserSelectedBitmap); targetPictureBox.EncodedImage = convertedImage; imageCaptureForm.Close(); } } ShowAllForms(); if (minimizePreference) { settings.ClientSettings.MinimizeToTray = true; settings.Save(settings); } }
public ImageElement FindImageElement(Bitmap smallBmp, double accuracy) { UIControlsHelper.HideAllForms(); bool testMode = TestMode; dynamic element = null; double tolerance = 1.0 - accuracy; Bitmap bigBmp = ImageMethods.Screenshot(); Bitmap smallTestBmp = new Bitmap(smallBmp); Bitmap bigTestBmp = new Bitmap(bigBmp); Graphics bigTestGraphics = Graphics.FromImage(bigTestBmp); BitmapData smallData = smallBmp.LockBits(new Rectangle(0, 0, smallBmp.Width, smallBmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); BitmapData bigData = bigBmp.LockBits(new Rectangle(0, 0, bigBmp.Width, bigBmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); int smallStride = smallData.Stride; int bigStride = bigData.Stride; int bigWidth = bigBmp.Width; int bigHeight = bigBmp.Height - smallBmp.Height + 1; int smallWidth = smallBmp.Width * 3; int smallHeight = smallBmp.Height; int margin = Convert.ToInt32(255.0 * tolerance); unsafe { byte *pSmall = (byte *)(void *)smallData.Scan0; byte *pBig = (byte *)(void *)bigData.Scan0; int smallOffset = smallStride - smallBmp.Width * 3; int bigOffset = bigStride - bigBmp.Width * 3; bool matchFound = true; for (int y = 0; y < bigHeight; y++) { for (int x = 0; x < bigWidth; x++) { byte *pBigBackup = pBig; byte *pSmallBackup = pSmall; //Look for the small picture. for (int i = 0; i < smallHeight; i++) { int j = 0; matchFound = true; for (j = 0; j < smallWidth; j++) { //With tolerance: pSmall value should be between margins. int inf = pBig[0] - margin; int sup = pBig[0] + margin; if (sup < pSmall[0] || inf > pSmall[0]) { matchFound = false; break; } pBig++; pSmall++; } if (!matchFound) { break; } //We restore the pointers. pSmall = pSmallBackup; pBig = pBigBackup; //Next rows of the small and big pictures. pSmall += smallStride * (1 + i); pBig += bigStride * (1 + i); } //If match found, we return. if (matchFound) { element = new ImageElement { LeftX = x, MiddleX = x + smallBmp.Width / 2, RightX = x + smallBmp.Width, TopY = y, MiddleY = y + smallBmp.Height / 2, BottomY = y + smallBmp.Height }; if (testMode) { //draw on output to demonstrate finding var Rectangle = new Rectangle(x, y, smallBmp.Width - 1, smallBmp.Height - 1); Pen pen = new Pen(Color.Red); pen.Width = 5.0F; bigTestGraphics.DrawRectangle(pen, Rectangle); frmImageCapture captureOutput = new frmImageCapture(); captureOutput.pbTaggedImage.Image = smallTestBmp; captureOutput.pbSearchResult.Image = bigTestBmp; captureOutput.TopMost = true; captureOutput.Show(); } break; } //If no match found, we restore the pointers and continue. else { pBig = pBigBackup; pSmall = pSmallBackup; pBig += 3; } } if (matchFound) { break; } pBig += bigOffset; } } bigBmp.UnlockBits(bigData); smallBmp.UnlockBits(smallData); bigTestGraphics.Dispose(); return(element); }