/// <summary> /// Resize the image to the specified width and height. /// </summary> /// <param name="image">The image to resize.</param> /// <param name="width">The width to resize to.</param> /// <param name="height">The height to resize to.</param> /// <returns>The resized image.</returns> public static System.Drawing.Bitmap ResizeImage(OcrEngineProfile profile, System.Drawing.Image image, int width, int height) { //a holder for the result Bitmap result = new Bitmap(width, height); //set the resolutions the same to avoid cropping due to resolution differences result.SetResolution(image.HorizontalResolution, image.VerticalResolution); //use a graphics object to draw the resized image into the bitmap using (Graphics graphics = Graphics.FromImage(result)) { //graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; //graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; //set the resize quality modes to high quality switch (profile) { case OcrEngineProfile.Linear: graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; break; case OcrEngineProfile.Bilinear: graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; break; case OcrEngineProfile.Bicubic: case OcrEngineProfile.Soft: default: graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; break; } //draw the image into the target bitmap //graphics.DrawImage(image, 0, 0, result.Width, result.Height); using (var ia = new ImageAttributes()) { ia.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); graphics.DrawImage(image, new Rectangle(0, 0, result.Width, result.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, ia); } } //return the resulting bitmap return(result); }
private Image ProcessImage(Image image, int imageProcess) { int index = 0; int scale = 1; OcrEngineProfile profile = OcrEngineProfile.Default; foreach (var value in Enum.GetValues(typeof(OcrEngineProfile))) { scale = 1; if ((OcrEngineProfile)value >= OcrEngineProfile.Linear) { for (int i = 2; i <= Program.ProfilesCount; i++) { scale = i; if (index == imageProcess) { break; } index++; } if (index == imageProcess) { profile = (OcrEngineProfile)value; break; } } else { if (index == imageProcess) { profile = (OcrEngineProfile)value; break; } index++; } } int width = image.Width * scale; int height = image.Height * scale; if (scale == 1 || (OcrEngineProfile)imageProcess < OcrEngineProfile.Linear) { return(new Bitmap(image)); } using (Image grey = ImageUtilities.MakeGrayscale(image)) { if (profile == OcrEngineProfile.Soft) { int width2 = image.Width * 2; int height2 = image.Width * 2; // Scale * 2 // Scale back to * 1 // Scale * profile size using (Image doubleImage = ImageUtilities.ResizeImage(profile, grey, width2, height2)) { using (Image normalImage = ImageUtilities.ResizeImage(profile, doubleImage, image.Width, image.Height)) { return(ImageUtilities.ResizeImage(profile, normalImage, width, height)); } } } return(ImageUtilities.ResizeImage(profile, image, width, height)); } }
public void Initialize() { isUpdating = true; Program.SetEngines(engines); Program.SetTranslators(translators); foreach (var value in Enum.GetValues(typeof(OcrEngineType))) { OcrEngineType type = (OcrEngineType)value; OcrEngine engine = OcrEngine.Create(type); if (engine != null)// && engine.IsInstalled) { engines[type] = engine; ocrEngineToolStripComboBox.Items.Add(type); } } if (ocrEngineToolStripComboBox.Items.Count > 0) { ocrEngineToolStripComboBox.SelectedIndex = 0; } foreach (var value in Enum.GetValues(typeof(TranslatorType))) { TranslatorType type = (TranslatorType)value; Translator translator = Translator.Create(type); if (translator != null && translator.IsEnabled) { translators[type] = translator; translateApiToolStripComboBox.Items.Add(type); } } if (translateApiToolStripComboBox.Items.Count > 0) { translateApiToolStripComboBox.SelectedIndex = 0; } foreach (var value in Enum.GetValues(typeof(OcrEngineProfile))) { OcrEngineProfile profile = (OcrEngineProfile)value; if (profile < OcrEngineProfile.Linear) { ocrProfileToolStripComboBox.Items.Add(profile); } else { for (int i = 2; i <= Program.ProfilesCount; i++) { ocrProfileToolStripComboBox.Items.Add(profile + " " + i + "x"); } } } if (ocrProfileToolStripComboBox.Items.Count > 0) { ocrProfileToolStripComboBox.SelectedIndex = 0; } UpdateLanguages(); ocrEngineToolStripComboBox.ComboBox.PreviewKeyDown += ComboBox_PreviewKeyDown; ocrProfileToolStripComboBox.ComboBox.PreviewKeyDown += ComboBox_PreviewKeyDown; translateApiToolStripComboBox.ComboBox.PreviewKeyDown += ComboBox_PreviewKeyDown; translateFromToolStripComboBox.ComboBox.PreviewKeyDown += ComboBox_PreviewKeyDown; translateToToolStripComboBox.ComboBox.PreviewKeyDown += ComboBox_PreviewKeyDown; ocrEngineToolStripComboBox.ComboBox.MouseWheel += toolStripComboBox_MouseWheel; ocrProfileToolStripComboBox.ComboBox.MouseWheel += toolStripComboBox_MouseWheel; translateApiToolStripComboBox.ComboBox.MouseWheel += toolStripComboBox_MouseWheel; translateFromToolStripComboBox.ComboBox.MouseWheel += toolStripComboBox_MouseWheel; translateToToolStripComboBox.ComboBox.MouseWheel += toolStripComboBox_MouseWheel; comboBoxPrevIndex[ocrEngineToolStripComboBox] = 0; comboBoxPrevIndex[ocrProfileToolStripComboBox] = 0; comboBoxPrevIndex[translateApiToolStripComboBox] = 0; comboBoxPrevIndex[translateFromToolStripComboBox] = 0; comboBoxPrevIndex[translateToToolStripComboBox] = 0; translateFromToolStripComboBox.ComboBox.DrawItem += LanguageComboBox_DrawItem; translateFromToolStripComboBox.ComboBox.DrawMode = DrawMode.OwnerDrawFixed; translateToToolStripComboBox.ComboBox.DrawItem += LanguageComboBox_DrawItem; translateToToolStripComboBox.ComboBox.DrawMode = DrawMode.OwnerDrawFixed; ocrEngineToolStripComboBox.ComboBox.DrawItem += EngineComboBox_DrawItem; ocrEngineToolStripComboBox.ComboBox.DrawMode = DrawMode.OwnerDrawFixed; ocrImagePanel.ImageChanged += ocrImagePanel_ImageChanged; OcrHelper.Complete += OcrHelper_Complete; OcrHelper.Start += OcrHelper_Start; TranslateHelper.Start += TranslateHelper_Start; TranslateHelper.Complete += TranslateHelper_Complete; string from = Program.Settings.LanguageProfiles.Slot1; string to = Program.Settings.TargetLanguage.TargetLanguage; if (!string.IsNullOrEmpty(from) && !string.IsNullOrEmpty(to)) { int fromIndex = translateFromToolStripComboBox.Items.IndexOf(from); int toIndex = translateToToolStripComboBox.Items.IndexOf(to); if (fromIndex >= 0 && toIndex >= 0) { translateFromToolStripComboBox.SelectedIndex = fromIndex; translateToToolStripComboBox.SelectedIndex = toIndex; } } hotkeys.KeyPressed += hotkeys_KeyPressed; hotkeys.ClearHotKey(); hotkeys.RegisterHotKey(Program.Settings.AutoTyper.Hotkey); isUpdating = false; SettingsChanged(); OcrNetwork.Update(); }