private bool HtmlImgCompare(string html, string imgPath) { string temphtmlPath = SpecialDirectories.Temp + "\\SUITemp.html"; if (File.Exists(imgPath)) { SUIBitmap standardImg = SUIBitmap.LoadSUIBitmap(imgPath); if (standardImg == null) { throw (new SUIHtmlCompareException()); } try { SUIUtil.OutputFileFromString(html, temphtmlPath); SUIIE ie = SUIIE.CreateIEWithURL(temphtmlPath); SUIBitmap tempImg = ie.GetClientArea(); // Fix a bug: we need to assign image path to the captured temp SUIBitmap object. tempImg.Path = imgPath; //SUIIE.CleanIEProcess(); SUIImageComparer Comparer = new SUIImageComparer(); return(Comparer.Compare(standardImg, tempImg)); }catch (SUIException e) { throw(new SUIHtmlCompareException(e)); } } else { try { SUIUtil.OutputFileFromString(html, temphtmlPath); SUIIE ie = SUIIE.CreateIEWithURL(temphtmlPath); SUIBitmap tempImg = ie.GetClientArea(); //SUIIE.CleanIEProcess(); tempImg.Path = imgPath; tempImg.Save(); return(true); } catch (SUIException e) { throw (new SUIHtmlCompareException(e)); } } }
public override bool Compare(Object obj1, Object obj2) { if (obj1 != null && obj2 != null) { if (obj1 is SUIBitmap && obj2 is SUIBitmap) { bool bFlag = OptionCompare(obj1 as SUIBitmap, obj2 as SUIBitmap, Rectangle.Empty); return(bFlag); } } if (obj1 == null) { SUIBitmap temp = obj2 as SUIBitmap; if (!temp.IsNullBitmap) { temp.Save(); return(true); } } return(false); }
public static SUIBitmap GetImageFromWindow(SUIWindow sui, bool withCursor) { if (null != sui) { SUISleeper.Sleep(2000); //make sure paint compldte! //Rectangle rDialog = new Rectangle(); try { if (!sui.ClassName.Equals("#32768")) { sui.BringToTopMost(); sui.ShowShortCutIndicators(); if (!withCursor) { SUIWinAPIs.SetCursorPos(SUIWindow.DesktopWindow.Width / 2, 0);// Set Cursor at top mid of screen } } } catch (Exception e) { throw new SUIGetImageException("Win32 SDK Platform Exception!", e); } IntPtr hdcSrc = IntPtr.Zero; IntPtr hdcDest = IntPtr.Zero; IntPtr hBitmap = IntPtr.Zero; Bitmap image = null; try { //index to solve the problem of GDR+ when run IE 2 times in one case. index = ((index % 10) + 1); //SUIWinAPIs.GetWindowRect(sui.WindowHandle, out rDialog); int buffer = 0; if ((SUIUtil.IsCurrentOSVista || SUIUtil.IsCurrentOSXP) && sui.IsWinForm && sui.Maximized) { buffer = 8; // under vista need cut a little height of window } hdcSrc = SUIWinAPIs.GetWindowDC(sui.WindowHandle); hdcDest = SUIWinAPIs.CreateCompatibleDC(hdcSrc); hBitmap = SUIWinAPIs.CreateCompatibleBitmap(hdcSrc, sui.Width - sui.X, sui.Height - sui.Y - buffer); SUIWinAPIs.SelectObject(hdcDest, hBitmap); SUIWinAPIs.BitBlt(hdcDest, 0, 0, sui.Width - sui.X, sui.Height - sui.Y - buffer, hdcSrc, 0, 0, 0x00CC0020); image = new Bitmap(Image.FromHbitmap(hBitmap), Image.FromHbitmap(hBitmap).Width, Image.FromHbitmap(hBitmap).Height); //Draw cursor image if (withCursor) { int cursorX = 0, cursorY = 0; SUIBitmap cursorBmp = GetCursorImage(ref cursorX, ref cursorY); if (cursorBmp != null) { Bitmap cBmp = cursorBmp.Bitmap; Rectangle r = new Rectangle(cursorX - sui.X, cursorY - sui.Y, cBmp.Width, cBmp.Height); Graphics g = Graphics.FromImage(image); g.DrawImage(cBmp, r); g.Flush(); cBmp.Dispose(); } } image.Save(tmpFile + index + tmpFileSuffix); image.Dispose(); //SaveImg(image,tmpFile,ImageFormat.Bmp); }catch (Exception e) { throw new SUIGetImageException("Win32 SDK Platform Exception!", e); }finally { if (!hdcSrc.Equals(IntPtr.Zero)) { SUIWinAPIs.ReleaseDC(sui.WindowHandle, hdcSrc); } if (!hdcDest.Equals(IntPtr.Zero)) { SUIWinAPIs.DeleteDC(hdcDest); } if (!hBitmap.Equals(IntPtr.Zero)) { SUIWinAPIs.DeleteObject(hBitmap); } if (image != null) { image.Dispose(); } } SUIBitmap ImageOfWindow = SUIBitmap.LoadSUIBitmap(tmpFile + index + tmpFileSuffix); return(ImageOfWindow); } throw new SUIGetImageException("Parameter SUIWindow sui is Null"); }
public static void SaveSUIBitmap(SUIBitmap suiBitmap) { suiBitmap.Save(); }
public SUIBitmap(SUIBitmap suiBitmap) { this.bitmap = suiBitmap.bitmap; this.path = suiBitmap.path; }
public bool CompareWindowImage(SUIWindow window, SUIBitmap baseImage) { SUIBitmap windowImage = SUIImage.GetImageFromWindow(window); windowImage.Path = baseImage.Path; //2007 5-24 fhu add this line. if (baseImage.IsNullBitmap) { if (!windowImage.IsNullBitmap) { windowImage.Path = baseImage.Path; windowImage.Save(); windowImage.ReleaseMem(); return true; } } //Here's the Vista specific image comparison. // Both dialog and winform need to apply this algorithm. if ((SUIUtil.IsCurrentOSVista || SUIUtil.IsCurrentOSXP) && (window.IsDialog || window.IsWinForm)) { SUIDialog dialog = new SUIDialog(window); SUIButton defaultButton = dialog.DefaultPushButton; if (defaultButton == null) return SUIComparer.GetComparer(ComparerType.Image).Compare(baseImage,windowImage); else { Bitmap bmBase = baseImage.Bitmap; Bitmap bmTest = windowImage.Bitmap; int base_width = bmBase.Width; int base_height = bmBase.Height; int test_width = bmTest.Width; int test_height = bmTest.Height; //First of all, compare the size of images. if ((base_width != test_width) || (base_height != test_height)) { windowImage.Path = baseImage.Path.Replace(".bmp", "_error.bmp"); windowImage.Save(); windowImage.ReleaseMem(); return false; } //Retrieve button area. Rectangle defaultButtonRectangle = new Rectangle(); defaultButtonRectangle.X = defaultButton.X - dialog.X; defaultButtonRectangle.Y = defaultButton.Y - dialog.Y; defaultButtonRectangle.Width = defaultButton.Width - defaultButton.X; defaultButtonRectangle.Height = defaultButton.Height - defaultButton.Y; // Ignor the comparison of a 4-pixel width border so that we could // get rid of the side effect of dash-line rectangle on the button. Rectangle defaultButtonRectangleEx = new Rectangle(); defaultButtonRectangleEx.X = defaultButtonRectangle.X + 4; defaultButtonRectangleEx.Y = defaultButtonRectangle.Y + 4; defaultButtonRectangleEx.Width = defaultButtonRectangle.Width - 8; defaultButtonRectangleEx.Height = defaultButtonRectangle.Height - 8; //Ignor the effect of flatness under vista Rectangle flatnessRectangle1 = new Rectangle(); flatnessRectangle1.X = 0; flatnessRectangle1.Y = 0; flatnessRectangle1.Width = 6; flatnessRectangle1.Height = 6; Rectangle flatnessRectangle2 = new Rectangle(); flatnessRectangle2.X = dialog.Width - dialog.X - 6; flatnessRectangle2.Y = 0; flatnessRectangle2.Width = 6; flatnessRectangle2.Height = 6; Rectangle[] FlatnessAreas = new Rectangle[2]; FlatnessAreas[0] = flatnessRectangle1; FlatnessAreas[1] = flatnessRectangle2; Bitmap difference = new Bitmap(base_width, base_height); bool bChange = false; int i, j; Color pixBase; Color pixTest; for (i = 0; i < base_width; i++) { for (j = 0; j < base_height; j++) { pixBase = bmBase.GetPixel(i, j); pixTest = bmTest.GetPixel(i, j); Point temp = new Point(i, j); if (IsContainsPoint(FlatnessAreas,temp)) { //difference.SetPixel(i, j, Color.White); continue; } if (!defaultButtonRectangle.Contains(temp)) { if (!colorComparer.Compare(pixBase, pixTest)) { bChange = true; difference.SetPixel(i, j, Color.Black); } else difference.SetPixel(i, j, Color.White); } else if (!defaultButtonRectangleEx.Contains(temp)) { // Ignor the pixels in this border. //difference.SetPixel(i, j, Color.Orange); } else //Process the pixels in OK button rectangle { if (!colorComparer.Compare(pixBase, pixTest)) { int defaultBuffer = SUIColorComparer.buffer; //Temproraly set buffer to 150 since in this special area we need a larger buffer. SUIColorComparer.buffer = 150; if (colorComparer.Compare(pixBase, SystemColors.ControlText) || colorComparer.Compare(pixTest, SystemColors.ControlText)) { bChange = true; difference.SetPixel(i, j, Color.Black); } //else // difference.SetPixel(i, j, Color.Blue); // Set the default buffer back. SUIColorComparer.buffer = defaultBuffer; } //else // difference.SetPixel(i, j, Color.Red); } } } if (bChange) { windowImage.Path = baseImage.Path.Replace(".bmp", "_error.bmp"); windowImage.Save(); windowImage.ReleaseMem(); baseImage.ReleaseMem(); string path = baseImage.Path.Replace(".bmp", "_error_difference.bmp"); difference.Save(path); difference.Dispose(); return false; } else { windowImage.ReleaseMem(); baseImage.ReleaseMem(); difference.Dispose(); return true; } } } else return SUIComparer.GetComparer(ComparerType.Image).Compare(baseImage, windowImage); }
/*do not compare some areas*/ public bool OptionCompare3(SUIBitmap SUIbmBase, SUIBitmap SUIbmTest, Rectangle[] specialAreas) { if (SUIbmBase.IsNullBitmap) { SUIBitmap temp = SUIbmTest as SUIBitmap; if (!temp.IsNullBitmap) { temp.Path = SUIbmBase.Path; temp.Save(); temp.ReleaseMem(); return true; } } bool bFlag = false; Bitmap bmBase = SUIbmBase.Bitmap; Bitmap bmTest = SUIbmTest.Bitmap; int base_width = bmBase.Width; int base_height = bmBase.Height; int test_width = bmTest.Width; int test_height = bmTest.Height; if ((base_width != test_width) || (base_height != test_height)) { SUIbmTest.Path = SUIbmBase.Path.Replace(".bmp", "_error.bmp"); SUIbmTest.Save(); SUIbmTest.ReleaseMem(); return bFlag; } int i, j; Color pixBase; Color pixTest; Bitmap difference = new Bitmap(base_width, base_height); bool bChange = false; for (i = 0; i < base_width; i++) { for (j = 0; j < base_height; j++) { pixBase = bmBase.GetPixel(i, j); pixTest = bmTest.GetPixel(i, j); Point temp = new Point(i, j); if (!IsContainsPoint(specialAreas,temp)) { if (!colorComparer.Compare(pixBase, pixTest)) { difference.SetPixel(i, j, Color.Black); bChange = true; } else { difference.SetPixel(i,j,Color.White); } } else { //difference.SetPixel(i,j,Color.White); } } } if (bChange) { SUIbmTest.Path = SUIbmBase.Path.Replace(".bmp", "_error.bmp"); SUIbmTest.Save(); SUIbmTest.ReleaseMem(); string path = SUIbmTest.Path.Replace(".bmp", "_error_difference.bmp"); ; difference.Save(path); return bFlag; } bFlag = true; return bFlag; }
public bool CompareWindowImage(SUIWindow window, SUIBitmap baseImage) { SUIBitmap windowImage = SUIImage.GetImageFromWindow(window); windowImage.Path = baseImage.Path; //2007 5-24 fhu add this line. if (baseImage.IsNullBitmap) { if (!windowImage.IsNullBitmap) { windowImage.Path = baseImage.Path; windowImage.Save(); windowImage.ReleaseMem(); return(true); } } //Here's the Vista specific image comparison. // Both dialog and winform need to apply this algorithm. if ((SUIUtil.IsCurrentOSVista || SUIUtil.IsCurrentOSXP) && (window.IsDialog || window.IsWinForm)) { SUIDialog dialog = new SUIDialog(window); SUIButton defaultButton = dialog.DefaultPushButton; if (defaultButton == null) { return(SUIComparer.GetComparer(ComparerType.Image).Compare(baseImage, windowImage)); } else { Bitmap bmBase = baseImage.Bitmap; Bitmap bmTest = windowImage.Bitmap; int base_width = bmBase.Width; int base_height = bmBase.Height; int test_width = bmTest.Width; int test_height = bmTest.Height; //First of all, compare the size of images. if ((base_width != test_width) || (base_height != test_height)) { windowImage.Path = baseImage.Path.Replace(".bmp", "_error.bmp"); windowImage.Save(); windowImage.ReleaseMem(); return(false); } //Retrieve button area. Rectangle defaultButtonRectangle = new Rectangle(); defaultButtonRectangle.X = defaultButton.X - dialog.X; defaultButtonRectangle.Y = defaultButton.Y - dialog.Y; defaultButtonRectangle.Width = defaultButton.Width - defaultButton.X; defaultButtonRectangle.Height = defaultButton.Height - defaultButton.Y; // Ignor the comparison of a 4-pixel width border so that we could // get rid of the side effect of dash-line rectangle on the button. Rectangle defaultButtonRectangleEx = new Rectangle(); defaultButtonRectangleEx.X = defaultButtonRectangle.X + 4; defaultButtonRectangleEx.Y = defaultButtonRectangle.Y + 4; defaultButtonRectangleEx.Width = defaultButtonRectangle.Width - 8; defaultButtonRectangleEx.Height = defaultButtonRectangle.Height - 8; //Ignor the effect of flatness under vista Rectangle flatnessRectangle1 = new Rectangle(); flatnessRectangle1.X = 0; flatnessRectangle1.Y = 0; flatnessRectangle1.Width = 6; flatnessRectangle1.Height = 6; Rectangle flatnessRectangle2 = new Rectangle(); flatnessRectangle2.X = dialog.Width - dialog.X - 6; flatnessRectangle2.Y = 0; flatnessRectangle2.Width = 6; flatnessRectangle2.Height = 6; Rectangle[] FlatnessAreas = new Rectangle[2]; FlatnessAreas[0] = flatnessRectangle1; FlatnessAreas[1] = flatnessRectangle2; Bitmap difference = new Bitmap(base_width, base_height); bool bChange = false; int i, j; Color pixBase; Color pixTest; for (i = 0; i < base_width; i++) { for (j = 0; j < base_height; j++) { pixBase = bmBase.GetPixel(i, j); pixTest = bmTest.GetPixel(i, j); Point temp = new Point(i, j); if (IsContainsPoint(FlatnessAreas, temp)) { //difference.SetPixel(i, j, Color.White); continue; } if (!defaultButtonRectangle.Contains(temp)) { if (!colorComparer.Compare(pixBase, pixTest)) { bChange = true; difference.SetPixel(i, j, Color.Black); } else { difference.SetPixel(i, j, Color.White); } } else if (!defaultButtonRectangleEx.Contains(temp)) { // Ignor the pixels in this border. //difference.SetPixel(i, j, Color.Orange); } else //Process the pixels in OK button rectangle { if (!colorComparer.Compare(pixBase, pixTest)) { int defaultBuffer = SUIColorComparer.buffer; //Temproraly set buffer to 150 since in this special area we need a larger buffer. SUIColorComparer.buffer = 150; if (colorComparer.Compare(pixBase, SystemColors.ControlText) || colorComparer.Compare(pixTest, SystemColors.ControlText)) { bChange = true; difference.SetPixel(i, j, Color.Black); } //else // difference.SetPixel(i, j, Color.Blue); // Set the default buffer back. SUIColorComparer.buffer = defaultBuffer; } //else // difference.SetPixel(i, j, Color.Red); } } } if (bChange) { windowImage.Path = baseImage.Path.Replace(".bmp", "_error.bmp"); windowImage.Save(); windowImage.ReleaseMem(); baseImage.ReleaseMem(); string path = baseImage.Path.Replace(".bmp", "_error_difference.bmp"); difference.Save(path); difference.Dispose(); return(false); } else { windowImage.ReleaseMem(); baseImage.ReleaseMem(); difference.Dispose(); return(true); } } } else { return(SUIComparer.GetComparer(ComparerType.Image).Compare(baseImage, windowImage)); } }
/*do not compare some areas*/ public bool OptionCompare3(SUIBitmap SUIbmBase, SUIBitmap SUIbmTest, Rectangle[] specialAreas) { if (SUIbmBase.IsNullBitmap) { SUIBitmap temp = SUIbmTest as SUIBitmap; if (!temp.IsNullBitmap) { temp.Path = SUIbmBase.Path; temp.Save(); temp.ReleaseMem(); return(true); } } bool bFlag = false; Bitmap bmBase = SUIbmBase.Bitmap; Bitmap bmTest = SUIbmTest.Bitmap; int base_width = bmBase.Width; int base_height = bmBase.Height; int test_width = bmTest.Width; int test_height = bmTest.Height; if ((base_width != test_width) || (base_height != test_height)) { SUIbmTest.Path = SUIbmBase.Path.Replace(".bmp", "_error.bmp"); SUIbmTest.Save(); SUIbmTest.ReleaseMem(); return(bFlag); } int i, j; Color pixBase; Color pixTest; Bitmap difference = new Bitmap(base_width, base_height); bool bChange = false; for (i = 0; i < base_width; i++) { for (j = 0; j < base_height; j++) { pixBase = bmBase.GetPixel(i, j); pixTest = bmTest.GetPixel(i, j); Point temp = new Point(i, j); if (!IsContainsPoint(specialAreas, temp)) { if (!colorComparer.Compare(pixBase, pixTest)) { difference.SetPixel(i, j, Color.Black); bChange = true; } else { difference.SetPixel(i, j, Color.White); } } else { //difference.SetPixel(i,j,Color.White); } } } if (bChange) { SUIbmTest.Path = SUIbmBase.Path.Replace(".bmp", "_error.bmp"); SUIbmTest.Save(); SUIbmTest.ReleaseMem(); string path = SUIbmTest.Path.Replace(".bmp", "_error_difference.bmp");; difference.Save(path); return(bFlag); } bFlag = true; return(bFlag); }