//------------------------------------------------------ // // Public Properties // //------------------------------------------------------ /// <summary> /// Compares to bitmaps to determine whether they are equal. /// </summary> /// <param name='master'>Master image for comparison.</param> /// <param name='sample'>Sampled image for comparison.</param> /// <param name='differences'> /// On return, bitmap with differences highlighted if any were found, /// null otherwise. /// </param> /// <param name='differenceLog'> /// On return, text description of any differences that were found, /// string.empty otherwise.</param> /// <returns>true if master and sample are equal, false otherwise.</returns> internal static bool AreBitmapsEqual(Bitmap master, Bitmap sample, out Bitmap differences, out string differenceLog) { ComparisonOperation op = new ComparisonOperation(); op.MasterImage = master; op.SampleImage = sample; ComparisonResult result = op.Execute(); if (result.CriteriaMet) { differences = null; differenceLog = string.Empty; return(true); } else { differences = new Bitmap(sample); result.HighlightDifferences(differences); // Use ToString rather than ToStringBrief - there will not be multiple // differences in a quick strict comparison. differenceLog = result.ToString(); return(false); } }
/// <summary> /// Executes the comparison operation with supporting services. /// </summary> /// <param name="prefixName"> /// Prefix name for criteria and operation configuration arguments. /// </param> /// <returns>The results of the comparison operation.</returns> /// <remarks><p> /// This method implements the typical wrapping around /// the comparison API, where services are available to help /// with configuration and logging. /// </p><p> /// The operation is configured, and a new criteria object is /// configured and assigned unless the current criteria has /// already been modified in some way. If the criteria is not /// met, an exception is thrown. If LogImagesOnUnmet is true and /// the criteria is not met, the images will be logged. /// </p><p> /// The ComparisonOperation object and the ComparisonResult /// object are configured with the /// ConfigurationSettings.SetObjectProperties method; see /// this API for more information. /// </p></remarks> public ComparisonResult ExecuteServiced(string prefixName) { #if (IGNORE_IMAGE_LOGGING) throw new NotImplementedException("ComparisonOperation.ExecuteServiced is not implemented when IGNORE_IMAGE_LOGGING is defined."); #else if (prefixName == null) { throw new ArgumentNullException("prefixName"); } GlobalLog.LogStatus("Performing {" + prefixName + "} comparison..."); if (Criteria.Equals(ComparisonCriteria.PerfectMatch)) { // Doing a SetObjectProperties call on this.criteria // would modify the properties of the shared perfect match // instance. ComparisonCriteria newCriteria = new ComparisonCriteria(); //ConfigurationSettings.Current // .SetObjectProperties(newCriteria, prefixName); Criteria = newCriteria; GlobalLog.LogStatus(criteria.ToString()); } ComparisonResult result = Execute(); if (!result.CriteriaMet) { if (LogImagesOnCriteriaUnmet) { masterImage.Save(prefixName + "master", System.Drawing.Imaging.ImageFormat.Png); GlobalLog.LogFile(prefixName + "master"); //l.LogImage(masterImage, prefixName + "master"); sampleImage.Save(prefixName + "sample", System.Drawing.Imaging.ImageFormat.Png); GlobalLog.LogFile(prefixName + "sample"); //l.LogImage(sampleImage, prefixName + "sample"); Bitmap differences = new Bitmap(sampleImage); result.HighlightDifferences(differences); differences.Save(prefixName + "differences", System.Drawing.Imaging.ImageFormat.Png); GlobalLog.LogFile(prefixName + "differences"); //l.LogImage(differences, prefixName + "differences"); } string message = criteria.MismatchDescription + Environment.NewLine + result.ToString(); throw new Exception(message); } else { GlobalLog.LogStatus(result.ToStringBrief()); } return(result); #endif }