public override void RunCommand(object sender) { var engine = (AutomationEngineInstance)sender; string vFolderPath = v_FolderPath.ConvertUserVariableToString(engine); string vFileName = v_FileName.ConvertUserVariableToString(engine); string vFilePath = Path.Combine(vFolderPath, vFileName); Bitmap image; if (v_ScreenshotWindowName == "Current Window") { image = ImageMethods.Screenshot(); } else { image = User32Functions.CaptureWindow(v_ScreenshotWindowName); } image.Save(vFilePath); }
private void frmImageCapture_Load(object sender, EventArgs e) { if (pbTaggedImage.Image != null) { pnlMouseContainer.Hide(); return; } else { tabTestMode.Hide(); } FormBorderStyle = FormBorderStyle.None; Location = new Point(0, 0); WindowState = FormWindowState.Maximized; //Hide the Form Hide(); Thread.Sleep(1000); //Copy Image from the screen Bitmap printscreen = ImageMethods.Screenshot(); //Create a temporal memory stream for the image using (MemoryStream s = new MemoryStream()) { //save graphic variable into memory printscreen.Save(s, ImageFormat.Bmp); pbMainImage.Size = new Size(Width, Height); //set the picture box with temporary stream pbMainImage.Image = Image.FromStream(s); } //Show Form Show(); //Cross Cursor Cursor = Cursors.Cross; }
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); }
public static ImageElement FindImageElement(Bitmap smallBmp, double accuracy, IAutomationEngineInstance engine, DateTime timeToEnd, bool isCaptureTest = false) { FormsHelper.HideAllForms(); var lastRecordedTime = DateTime.Now; 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++) { if (engine != null && engine.IsCancellationPending) { break; } if (engine != null && lastRecordedTime.Second != DateTime.Now.Second) { engine.ReportProgress("Element Not Yet Found... " + (timeToEnd - DateTime.Now).Seconds + "s remain"); lastRecordedTime = DateTime.Now; } if (timeToEnd <= DateTime.Now) { break; } 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 (isCaptureTest) { element.SmallTestImage = smallTestBmp; element.BigTestImage = bigTestBmp; } 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); }