using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; #if WIN32 using MSHTML; #else using mshtml; #endif using System.IO; using System.Drawing.Imaging; using System.Net; using System.Collections.Generic; namespace sharpknife.Utils { public class ImageUtils { public enum MatchType { None, Upside, Down } [DllImport("gdi32")] public static extern int DeleteObject(IntPtr hObject); public static ImageSource BitmapToImageSource(Bitmap bitmap) { var hbitmap = bitmap.GetHbitmap(); try { var imageSource = Imaging.CreateBitmapSourceFromHBitmap( hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bitmap.Width, bitmap.Height)); return imageSource; } finally { DeleteObject(hbitmap); } } public static BitmapImage GetBitmapImage(Bitmap image) { MemoryStream ms = new MemoryStream(); image.Save(ms, ImageFormat.Png); ms.Position = 0; BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = ms; bi.EndInit(); return bi; } [ComImport, InterfaceType((short)1), Guid("3050F669-98B5-11CF-BB82-00AA00BDCE0B")] private interface IHTMLElementRenderFixed { void DrawToDC(IntPtr hdc); void SetDocumentPrinter(string bstrPrinterName, IntPtr hdc); } public static Bitmap GetImage(IHTMLImgElement img) { try { IHTMLElementRenderFixed render = (IHTMLElementRenderFixed)img; Bitmap bmp = new Bitmap(img.width, img.height); Graphics g = Graphics.FromImage(bmp); IntPtr hdc = g.GetHdc(); render.DrawToDC(hdc); g.ReleaseHdc(hdc); return bmp; } catch (Exception ex) { LogItem item = new LogItem(ex.ToString()); item.Save(); } return null; } public static Image[] Split(Image image, Rectangle size) { Bitmap bitmap = new Bitmap(image); int x = image.Width / size.Width; int y = image.Height / size.Height; Image[] split = new Image[x * y]; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { Rectangle crop = new Rectangle(i * size.Width, j * size.Height, size.Width, size.Height); try { split[j * y + i] = bitmap.Clone(crop, bitmap.PixelFormat); } catch (Exception ex) { LogItem item = new LogItem(ex.ToString()); item.Save(); } } } return split; } public static bool Compare(Bitmap bmp1, Bitmap bmp2) { MemoryStream ms = new MemoryStream(); bmp1.Save(ms, System.Drawing.Imaging.ImageFormat.Png); String firstBitmap = Convert.ToBase64String(ms.ToArray()); ms.Position = 0; bmp2.Save(ms, System.Drawing.Imaging.ImageFormat.Png); String secondBitmap = Convert.ToBase64String(ms.ToArray()); if (firstBitmap.Equals(secondBitmap)) { return true; } else { return false; } } public static Bitmap BitmapFromWeb(string URL) { try { // create a web request to the url of the image HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL); // set the method to GET to get the image myRequest.Method = "GET"; // get the response from the webpage HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); // create a bitmap from the stream of the response Bitmap bmp = new Bitmap(myResponse.GetResponseStream()); // close off the stream and the response myResponse.Close(); // return the Bitmap of the image return bmp; } catch (Exception ex) { return null; // if for some reason we couldn't get to image, we return null } } public static bool CompareImages(Bitmap img1, Bitmap img2) { return CompareImages(img1, img2, 0, 0, img1.Width, img2.Height, 10); } public static bool CompareImages(Bitmap img1, Bitmap img2, int startX, int startY, int width, int height, int tolerance) { return CompareImages(img1, img2, startX, startY, startX, startY, width, height, tolerance); } public static bool CompareImages(Bitmap img1, Bitmap img2, int leftX, int leftY, int rightX, int rightY, int width, int height, int tolerance) { int count = 0; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { string img1_ref = img1.GetPixel(i + leftX, j + leftY).ToString(); string img2_ref = img2.GetPixel(i + rightX, j + rightY).ToString(); if (img1_ref != img2_ref) { count++; } if (count > tolerance) { return false; } } } if (count > tolerance) { return false; } return true; } public static int ParseCaptcha(Bitmap image) { try { image = CleanImage(image); tessnet2.Tesseract ocr = new tessnet2.Tesseract(); ocr.SetVariable("tessedit_char_whitelist", "0123456789"); // If digit only ocr.Init("../../tessdata", "eng", false); // To use correct tessdata List result = ocr.DoOCR(image, Rectangle.Empty); foreach (tessnet2.Word word in result) { return int.Parse(word.Text); } } catch (Exception ex) { image.Save(string.Format("captcha_{0}.png", Guid.NewGuid())); //DateTime.Now.ToString("yyyyMMddhhmmss"))); } return -1; } public static string ParseStringCaptcha(Bitmap image) { try { tessnet2.Tesseract ocr = new tessnet2.Tesseract(); //ocr.SetVariable("tessedit_char_whitelist", "abcdefghijklmnopqrstuvwxyz"); // Chars only ocr.SetVariable("tessedit_char_whitelist", "almn"); // Chars only ocr.Init("../../tessdata", "eng", false); // To use correct tessdata List result = ocr.DoOCR(image, Rectangle.Empty); foreach (tessnet2.Word word in result) { return word.Text; } } catch (Exception ex) { string path = string.Format(@"..\..\Images\Captcha\Strings"); string filename = string.Format("{0}.png", Guid.NewGuid()); image.Save(Path.Combine(path, filename)); //image.Save(string.Format("captcha_{0}.png", Guid.NewGuid())); //DateTime.Now.ToString("yyyyMMddhhmmss"))); } return string.Empty; } public static void IncreaseContrast(Bitmap source, float contrastThreshold) { Graphics graphics = Graphics.FromImage(source); for (int i = 0; i < source.Width; i++) { for (int j = 0; j < source.Height; j++) { if (source.GetPixel(i, j).GetBrightness() > contrastThreshold) { source.SetPixel(i, j, System.Drawing.Color.White); } else { source.SetPixel(i, j, System.Drawing.Color.Black); } } } } public static void RemoveVerticalLines(Bitmap source, float lineThreshold, float brightnessThreshold) { bool[] lines = new bool[source.Width]; for (int i = 0; i < source.Width; i++) { float brightness = 0f; float[] brightnessArray = new float[source.Height]; for (int j = 0; j < source.Height; j++) { brightness += (1 - source.GetPixel(i, j).GetBrightness()); brightnessArray[j] = (1 - source.GetPixel(i, j).GetBrightness()); } if ((brightness / source.Height) > lineThreshold) { lines[i] = true; for (int j = 0; j < source.Height; j++) { if (ImageUtils.GetSurroundingBrightness(source, i, j) > brightnessThreshold) { source.SetPixel(i, j, System.Drawing.Color.White); } } } else { lines[i] = false; } } } public static void RemoveHorizontalLines(Bitmap source, float lineThreshold, float brightnessThreshold) { bool[] lines = new bool[source.Height]; for (int i = 0; i < source.Height; i++) { float brightness = 0f; float[] brightnessArray = new float[source.Width]; for (int j = 0; j < source.Width; j++) { brightness += (1 - source.GetPixel(j, i).GetBrightness()); brightnessArray[j] = (1 - source.GetPixel(j, i).GetBrightness()); } if ((brightness / source.Height) > lineThreshold) { lines[i] = true; for (int j = 0; j < source.Width; j++) { if (ImageUtils.GetSurroundingBrightness(source, j, i) > brightnessThreshold) { source.SetPixel(j, i, System.Drawing.Color.White); } } } else { lines[i] = false; } } } public static void RemoveNoise(Bitmap source, float noiseBrightness) { for (int i = 0; i < source.Width; i++) { for (int j = 0; j < source.Height; j++) { if (ImageUtils.GetSurroundingBrightness(source, i, j) > noiseBrightness) { source.SetPixel(i, j, System.Drawing.Color.White); } } } } public static Bitmap ZoomImage(Bitmap source, int zoom) { throw new Exception("Not working"); Graphics graphics = Graphics.FromImage(source); Image image = new Bitmap(source.Width * zoom, source.Height * zoom, System.Drawing.Imaging.PixelFormat.Format32bppArgb); graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; graphics.DrawImage(image, 0, 0, image.Width, image.Height); return (Bitmap)image; } private static Bitmap CleanImage(Bitmap source) { float contrastThreshold = 0.79f; float brightnessThreshold = 0.32f; float noiseThreshold = 0.49f; float lineThreshold = 0.50f; Bitmap image = (Bitmap)source.Clone(); Graphics graphics = Graphics.FromImage(image); // Increase contrast ImageUtils.IncreaseContrast(image, contrastThreshold); // Remove vertical lines ImageUtils.RemoveVerticalLines(image, lineThreshold, brightnessThreshold); // Remove horizontal lines ImageUtils.RemoveHorizontalLines(image, lineThreshold, brightnessThreshold); // Remove noise ImageUtils.RemoveNoise(image, noiseThreshold); #region Old // Increase contrast //for (int i = 0; i < image.Width; i++) //{ // for (int j = 0; j < image.Height; j++) // { // if (image.GetPixel(i, j).GetBrightness() > contrastThreshold) // { // image.SetPixel(i, j, System.Drawing.Color.White); // } // else // { // image.SetPixel(i, j, System.Drawing.Color.Black); // } // } //} // Remove vertical lines //for (int i = 0; i < image.Width; i++) //{ // System.Drawing.Color topPixel = image.GetPixel(i, 0); // System.Drawing.Color bottomPixel = image.GetPixel(i, image.Height - 1); // float brightness = 0f; // for (int j = 0; j < image.Height; j++) // { // brightness += image.GetPixel(i, j).GetBrightness(); // } // //if (topPixel.GetBrightness() < brightnessThreshold && bottomPixel.GetBrightness() < brightnessThreshold) // if ((brightness / image.Height) < brightnessThreshold) // { // for (int j = 0; j < image.Height; j++) // { // if (GetSurroundingBrightness(image, i, j) > noiseBrightness) // { // image.SetPixel(i, j, System.Drawing.Color.White); // } // } // //graphics.DrawLine(new Pen(Color.White), new PointF(i, 0), new PointF(i, image.Height)); // } //} // Remove horizontal lines //for (int i = 0; i < image.Height; i++) //{ // System.Drawing.Color leftPixel = image.GetPixel(0, i); // System.Drawing.Color rightPixel = image.GetPixel(image.Width - 1, i); // float brightness = 0f; // for (int j = 0; j < image.Width; j++) // { // brightness += image.GetPixel(j, i).GetBrightness(); // } // //if (leftPixel.GetBrightness() < brightnessThreshold && rightPixel.GetBrightness() < brightnessThreshold) // if ((brightness / image.Height) < brightnessThreshold) // { // for (int j = 0; j < image.Width; j++) // { // if (GetSurroundingBrightness(image, j, i) > noiseBrightness) // { // image.SetPixel(j, i, System.Drawing.Color.White); // } // } // //graphics.DrawLine(new Pen(Color.White), new PointF(0, i), new PointF(image.Width, i)); // } //} // Remove noise //for (int i = 0; i < image.Width; i++) //{ // for (int j = 0; j < image.Height; j++) // { // if (GetSurroundingBrightness(image, i, j) > noiseBrightness) // { // image.SetPixel(i, j, System.Drawing.Color.White); // } // } //} #endregion return image; } public static float GetSurroundingBrightness(Bitmap image, int x, int y) { float brightness = 0f; int counter = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (x + i >= 0 && x + i < image.Width && y + j >= 0 && y + j < image.Height) { counter++; brightness += image.GetPixel(x + i, y + j).GetBrightness(); } } } return brightness / counter; } public static Image Crop(Image img, Rectangle cropArea) { Bitmap bmpImage = new Bitmap(img); Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat); return (Image)(bmpCrop); } } }