public CompareItem(ManagedBitmap bmp, string name, bool isItalic, int expandCount, string text) { Bitmap = bmp; Name = name; Italic = isItalic; ExpandCount = expandCount; NumberOfForegroundColors = -1; Text = text; }
internal void DrawImage(ManagedBitmap bmp, Point point) { for (int y = 0; y < bmp.Height; y++) { int newY = point.Y + y; if (newY >= 0 && newY < Height) { for (int x = 0; x < bmp.Width; x++) { int newX = point.X + x; if (newX >= 0 && newX < Width) { this.SetPixel(newX, newY, bmp.GetPixel(x, y)); } } } } }
private void Ink_Load(object sender, System.EventArgs e) { inkCollector = new InkOverlay(this.drawingArea.Handle); inkCollector.AutoRedraw = false; inkCollector.DefaultDrawingAttributes.Width = 100; inkCollector.NewPackets += new InkCollectorNewPacketsEventHandler(inkCollector_NewPackets); inkCollector.Stroke += new InkCollectorStrokeEventHandler(inkCollector_Stroke); ManagedBitmap mbmp = new ManagedBitmap(0, 0, new Bitmap(mapsWidth, mapsHeight)); mbmp.g.DrawRectangle(Pens.LightGray, 0, 0, mapsWidth - 1, mapsHeight - 1); mbmp.g.DrawString("(" + getGridX(0) + ", " + getGridY(0) + ")", this.Font, Brushes.Black, 10f, 10f); managedMaps.Add(new Point(0, 0), mbmp); inkCollector.Enabled = true; }
private void ListBoxFileNamesSelectedIndexChanged(object sender, EventArgs e) { checkBoxItalic.Checked = _italics[listBoxFileNames.SelectedIndex]; string name = listBoxFileNames.Items[listBoxFileNames.SelectedIndex].ToString(); string databaseName = _directoryPath + "Images.db"; string posAsString = GetSelectedFileName(); Bitmap bmp = null; if (File.Exists(databaseName)) { using (var f = new FileStream(databaseName, FileMode.Open)) { if (name.Contains("]")) { name = name.Substring(name.IndexOf("]") + 1).Trim(); } f.Position = Convert.ToInt64(name); bmp = new ManagedBitmap(f).ToOldBitmap(); } } if (bmp == null) { bmp = new Bitmap(1, 1); labelImageInfo.Text = Configuration.Settings.Language.VobSubEditCharacters.ImageFileNotFound; } pictureBox1.Image = bmp; pictureBox2.Width = bmp.Width * 2; pictureBox2.Height = bmp.Height * 2; pictureBox2.Image = bmp; if (Additions != null && Additions.Count > 0) { string target = GetSelectedFileName(); foreach (var a in Additions) { if (target.StartsWith(a.Name)) { textBoxText.Text = a.Text; break; } } } }
private static WriteableBitmap _Get(string name) { lock (_lock) { if (name != _loadedName) { ManagedBitmap managedBitmap; using (var stream = new System.IO.FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); var frame = decoder.Frames[0]; managedBitmap = new ManagedBitmap(frame); } using (var lockedBitmap = managedBitmap.Lock()) { int height = lockedBitmap.Height; int width = lockedBitmap.Width; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { var color = lockedBitmap[x, y]; if (color.Red < 10 && color.Green < 10 && color.Blue < 10) { lockedBitmap[x, y] = new Argb(); } } } } _bitmap = managedBitmap.WriteableBitmap; _loadedName = name; } return(_bitmap); } }
/// <summary> /// Copies a rectangle from the bitmap to a new bitmap /// </summary> /// <param name="section">Source rectangle</param> /// <returns>Rectangle from current image as new bitmap</returns> public ManagedBitmap GetRectangle(Rectangle section) { ManagedBitmap newRectangle = new ManagedBitmap(section.Width, section.Height); int recty = 0; for (int y = section.Top; y < section.Top + section.Height; y++) { int rectx = 0; for (int x = section.Left; x < section.Left + section.Width; x++) { Color c = Color.Transparent; if (this.GetPixel(x, y) > 0) { c = Color.White; } newRectangle.SetPixel(rectx, recty, c); rectx++; } recty++; } return(newRectangle); }
private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e) { labelImageInfo.Text = string.Empty; labelExpandCount.Text = string.Empty; if (listBoxInspectItems.SelectedIndex < 0) { return; } _selectedCompareNode = null; _selectedCompareBinaryOcrBitmap = null; pictureBoxInspectItem.Image = _imageSources[listBoxInspectItems.SelectedIndex]; pictureBoxCompareBitmap.Image = null; pictureBoxCompareBitmapDouble.Image = null; int index = (listBoxInspectItems.SelectedIndex); var match = _matches[index]; _selectedMatch = match; if (!string.IsNullOrEmpty(match.Name)) { Bitmap bitmap = new Bitmap(1, 1); if (_binOcrDb != null) { bool bobFound = false; foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImages) { if (match.Name == bob.Key) { textBoxText.Text = bob.Text; checkBoxItalic.Checked = bob.Italic; _selectedCompareBinaryOcrBitmap = bob; bitmap = bob.ToOldBitmap(); pictureBoxCompareBitmap.Image = bitmap; pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2; pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2; pictureBoxCompareBitmapDouble.Image = bitmap; var matchBob = new BinaryOcrBitmap(new NikseBitmap(_imageSources[listBoxInspectItems.SelectedIndex])); if (matchBob.Hash == bob.Hash && matchBob.Width == bob.Width && matchBob.Height == bob.Height && matchBob.NumberOfColoredPixels == bob.NumberOfColoredPixels) { buttonAddBetterMatch.Enabled = false; // exact match } else { buttonAddBetterMatch.Enabled = true; } bobFound = true; break; } } if (!bobFound) { foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImagesExpanded) { if (match.Name == bob.Key) { textBoxText.Text = bob.Text; checkBoxItalic.Checked = bob.Italic; _selectedCompareBinaryOcrBitmap = bob; bitmap = bob.ToOldBitmap(); pictureBoxCompareBitmap.Image = bitmap; pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2; pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2; pictureBoxCompareBitmapDouble.Image = bitmap; buttonAddBetterMatch.Enabled = false; // exact match labelExpandCount.Text = string.Format("Expand count: {0}", bob.ExpandCount); break; } } } } else { foreach (XmlNode node in ImageCompareDocument.DocumentElement.ChildNodes) { if (node.Attributes["Text"] != null && node.InnerText == match.Name) { string text = node.Attributes["Text"].InnerText; string imageFileName = node.InnerText; imageFileName = Path.Combine(_directoryPath, imageFileName); textBoxText.Text = text; checkBoxItalic.Checked = node.Attributes["Italic"] != null; string databaseName = Path.Combine(_directoryPath, "Images.db"); using (var f = new FileStream(databaseName, FileMode.Open)) { try { string name = node.InnerText; int pos = Convert.ToInt32(name); f.Position = pos; ManagedBitmap mbmp = new ManagedBitmap(f); bitmap = mbmp.ToOldBitmap(); pictureBoxCompareBitmap.Image = bitmap; pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2; pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2; pictureBoxCompareBitmapDouble.Image = bitmap; } catch (Exception exception) { MessageBox.Show(exception.Message); } } try { labelImageInfo.Text = string.Format(Configuration.Settings.Language.VobSubEditCharacters.Image + " - {0}x{1}", bitmap.Width, bitmap.Height); } catch { } _selectedCompareNode = node; break; } } } } if (_selectedCompareNode == null && _selectedCompareBinaryOcrBitmap == null) { buttonUpdate.Enabled = false; buttonDelete.Enabled = false; buttonAddBetterMatch.Enabled = false; textBoxText.Enabled = false; textBoxText.Text = string.Empty; checkBoxItalic.Enabled = false; } else { buttonUpdate.Enabled = true; buttonDelete.Enabled = true; if (_selectedCompareNode != null) { buttonAddBetterMatch.Enabled = true; } textBoxText.Enabled = true; checkBoxItalic.Enabled = true; } }
internal void DrawImage(ManagedBitmap bmp, Point point) { for (int y = 0; y < bmp.Height; y++) { int newY = point.Y + y; if (newY >= 0 && newY < Height) { for (int x = 0; x < bmp.Width; x++) { int newX = point.X + x; if (newX >= 0 && newX < Width) this.SetPixel(newX, newY, bmp.GetPixel(x, y)); } } } }
/// <summary> /// Copies a rectangle from the bitmap to a new bitmap /// </summary> /// <param name="section">Source rectangle</param> /// <returns>Rectangle from current image as new bitmap</returns> public ManagedBitmap GetRectangle(Rectangle section) { ManagedBitmap newRectangle = new ManagedBitmap(section.Width, section.Height); int recty = 0; for (int y = section.Top; y < section.Top + section.Height; y++) { int rectx = 0; for (int x = section.Left; x < section.Left + section.Width; x++) { Color c = Color.Transparent; if (this.GetPixel(x, y) > 0) c = Color.White; newRectangle.SetPixel(rectx, recty, c); rectx++; } recty++; } return newRectangle; }
private void LoadOldCompareImages() { _compareBitmaps = new List<CompareItem>(); string path = Configuration.VobSubCompareFolder + comboBoxCharacterDatabase.SelectedItem + Path.DirectorySeparatorChar; if (!File.Exists(path + "CompareDescription.xml")) _compareDoc.LoadXml("<OcrBitmaps></OcrBitmaps>"); else _compareDoc.Load(path + "CompareDescription.xml"); string databaseName = path + "Images.db"; if (!File.Exists(databaseName)) { labelStatus.Text = Configuration.Settings.Language.VobSubOcr.LoadingImageCompareDatabase; labelStatus.Refresh(); using (var f = new FileStream(databaseName, FileMode.Create)) { foreach (string bmpFileName in Directory.GetFiles(path, "*.bmp")) { string name = Path.GetFileNameWithoutExtension(bmpFileName); XmlNode node = _compareDoc.DocumentElement.SelectSingleNode("FileName[.='" + name + "']"); if (node != null) { node.InnerText = f.Position.ToString(CultureInfo.InvariantCulture); var b = new Bitmap(bmpFileName); var m = new ManagedBitmap(b); b.Dispose(); m.AppendToStream(f); } } } _compareDoc.Save(path + "Images.xml"); string text = File.ReadAllText(path + "Images.xml"); File.WriteAllText(path + "Images.xml", text.Replace("<FileName", "<Item").Replace("</FileName>", "</Item>")); labelStatus.Text = string.Empty; } if (File.Exists(databaseName)) { labelStatus.Text = Configuration.Settings.Language.VobSubOcr.LoadingImageCompareDatabase; labelStatus.Refresh(); _compareDoc.Load(path + "Images.xml"); using (var f = new FileStream(databaseName, FileMode.Open)) { foreach (XmlNode node in _compareDoc.DocumentElement.SelectNodes("Item")) { try { string name = node.InnerText; int pos = Convert.ToInt32(name); bool isItalic = node.Attributes["Italic"] != null; string text = node.Attributes["Text"].InnerText; int expandCount = 0; if (node.Attributes["Expand"] != null) { if (!int.TryParse(node.Attributes["Expand"].InnerText, out expandCount)) expandCount = 0; } f.Position = pos; var mbmp = new ManagedBitmap(f); _compareBitmaps.Add(new CompareItem(mbmp, name, isItalic, expandCount, text)); } catch { //MessageBox.Show(node.OuterXml); } } } labelStatus.Text = string.Empty; } }
private void ListBoxFileNamesSelectedIndexChanged(object sender, EventArgs e) { checkBoxItalic.Checked = _italics[listBoxFileNames.SelectedIndex]; string name = listBoxFileNames.Items[listBoxFileNames.SelectedIndex].ToString(); string databaseName = _directoryPath + "Images.db"; Bitmap bmp = null; labelExpandCount.Text = string.Empty; labelImageInfo.Text = string.Empty; if (_binOcrDb != null) { var bob = GetSelectedBinOcrBitmap(); if (bob != null) { bmp = bob.ToOldBitmap(); labelImageInfo.Text = string.Format("Top:{0}, {1} colored pixels of {2}", bob.Y, bob.NumberOfColoredPixels, (bob.Width * bob.Height)); //bool italicI; //var isI = bob.IsLowercaseI(out italicI); //labelImageInfo.Text = string.Format("T:{0} j{1} :{2} i{3}{4} '{5} #{6}/{7}", bob.Y, bob.IsLowercaseJ(), bob.IsColon(), isI, italicI ? "i" : "", bob.IsApostrophe(), bob.NumberOfColoredPixels, (bob.Width * bob.Height)); if (bob.ExpandCount > 0) { labelExpandCount.Text = string.Format("Expand count: {0}", bob.ExpandCount); } } } else if (File.Exists(databaseName)) { using (var f = new FileStream(databaseName, FileMode.Open)) { if (name.Contains(']')) { name = name.Substring(name.IndexOf(']') + 1).Trim(); } f.Position = Convert.ToInt64(name); bmp = new ManagedBitmap(f).ToOldBitmap(); } } if (bmp == null) { bmp = new Bitmap(1, 1); labelImageInfo.Text = Configuration.Settings.Language.VobSubEditCharacters.ImageFileNotFound; } pictureBox1.Image = bmp; pictureBox1.Width = bmp.Width + 2; pictureBox1.Height = bmp.Height + 2; pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; var bmp2 = VobSubOcr.ResizeBitmap(bmp, bmp.Width * 2, bmp.Height * 2); pictureBox2.Image = bmp2; pictureBox2.Width = bmp2.Width + 2; pictureBox2.Height = bmp2.Height + 2; pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage; if (Additions != null && Additions.Count > 0) { if (_binOcrDb != null) { var bob = GetSelectedBinOcrBitmap(); foreach (var a in Additions) { if (bob != null && bob.Text != null && bob.Key == a.Name) { textBoxText.Text = a.Text; checkBoxItalic.Checked = a.Italic; break; } } } else { string target = GetSelectedFileName(); foreach (var a in Additions) { if (target.StartsWith(a.Name, StringComparison.Ordinal)) { textBoxText.Text = a.Text; break; } } } } }
private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e) { labelImageInfo.Text = string.Empty; labelExpandCount.Text = string.Empty; if (listBoxInspectItems.SelectedIndex < 0) { return; } _selectedCompareNode = null; _selectedCompareBinaryOcrBitmap = null; var img = _imageSources[listBoxInspectItems.SelectedIndex]; pictureBoxInspectItem.Image = img; if (img != null) { pictureBoxInspectItem.Width = img.Width + 2; pictureBoxInspectItem.Height = img.Height + 2; } pictureBoxCompareBitmap.Image = null; pictureBoxCompareBitmapDouble.Image = null; int index = listBoxInspectItems.SelectedIndex; var match = _matches[index]; _selectedMatch = match; if (!string.IsNullOrEmpty(match.Name)) { if (_binOcrDb != null) { bool bobFound = false; foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImages) { if (match.Name == bob.Key) { textBoxText.Text = bob.Text; checkBoxItalic.Checked = bob.Italic; _selectedCompareBinaryOcrBitmap = bob; SetDbItemView(bob.ToOldBitmap()); var matchBob = new BinaryOcrBitmap(new NikseBitmap(_imageSources[listBoxInspectItems.SelectedIndex])); if (matchBob.Hash == bob.Hash && matchBob.Width == bob.Width && matchBob.Height == bob.Height && matchBob.NumberOfColoredPixels == bob.NumberOfColoredPixels) { buttonAddBetterMatch.Enabled = false; // exact match } else { buttonAddBetterMatch.Enabled = true; } bobFound = true; break; } } if (!bobFound) { foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImagesExpanded) { if (match.Name == bob.Key) { textBoxText.Text = bob.Text; checkBoxItalic.Checked = bob.Italic; _selectedCompareBinaryOcrBitmap = bob; var oldBitmap = bob.ToOldBitmap(); SetDbItemView(oldBitmap); int dif = 1; if (oldBitmap.Width == match.ImageSplitterItem.NikseBitmap.Width && oldBitmap.Height == match.ImageSplitterItem.NikseBitmap.Height) { dif = NikseBitmapImageSplitter.IsBitmapsAlike(match.ImageSplitterItem.NikseBitmap, oldBitmap); } buttonAddBetterMatch.Enabled = dif > 0; // if exact match then don't allow "Add better match" labelExpandCount.Text = $"Expand count: {bob.ExpandCount}"; break; } } } } else { foreach (XmlNode node in ImageCompareDocument.DocumentElement.ChildNodes) { if (node.Attributes["Text"] != null && node.InnerText == match.Name) { string text = node.Attributes["Text"].InnerText; textBoxText.Text = text; checkBoxItalic.Checked = node.Attributes["Italic"] != null; string databaseName = Path.Combine(_directoryPath, "Images.db"); using (var f = new FileStream(databaseName, FileMode.Open)) { try { string name = node.InnerText; int pos = Convert.ToInt32(name); f.Position = pos; var mbmp = new ManagedBitmap(f); var bitmap = mbmp.ToOldBitmap(); SetDbItemView(mbmp.ToOldBitmap()); labelImageInfo.Text = string.Format(Configuration.Settings.Language.VobSubEditCharacters.Image + " - {0}x{1}", bitmap.Width, bitmap.Height); } catch (Exception exception) { labelImageInfo.Text = Configuration.Settings.Language.VobSubEditCharacters.Image; MessageBox.Show(exception.Message); } } _selectedCompareNode = node; break; } } } } buttonAddBetterMatch.Text = Configuration.Settings.Language.VobSubOcrCharacterInspect.AddBetterMatch; if (_selectedMatch.Text == Configuration.Settings.Language.VobSubOcr.NoMatch) { buttonUpdate.Enabled = false; buttonDelete.Enabled = false; buttonAddBetterMatch.Enabled = true; buttonAddBetterMatch.Text = Configuration.Settings.Language.VobSubOcrCharacterInspect.Add; textBoxText.Enabled = true; textBoxText.Text = string.Empty; checkBoxItalic.Enabled = true; pictureBoxCompareBitmap.Visible = true; pictureBoxCompareBitmapDouble.Visible = true; labelDoubleSize.Visible = true; } else if (_selectedCompareNode == null && _selectedCompareBinaryOcrBitmap == null) { buttonUpdate.Enabled = false; buttonDelete.Enabled = false; buttonAddBetterMatch.Enabled = true; textBoxText.Enabled = true; textBoxText.Text = string.Empty; checkBoxItalic.Enabled = false; pictureBoxCompareBitmap.Visible = false; pictureBoxCompareBitmapDouble.Visible = false; labelDoubleSize.Visible = false; if (img == null) { buttonAddBetterMatch.Enabled = false; } } else { buttonUpdate.Enabled = true; buttonDelete.Enabled = true; if (_selectedCompareNode != null) { buttonAddBetterMatch.Enabled = true; } textBoxText.Enabled = true; checkBoxItalic.Enabled = true; pictureBoxCompareBitmap.Visible = true; pictureBoxCompareBitmapDouble.Visible = true; labelDoubleSize.Visible = true; } }
private void inkCollector_Stroke(object sender, InkCollectorStrokeEventArgs e) { if (dryInk != null) { dryInk.Wait(); } dryInk = Task.Run(() => { Matrix originalMatrix = new Matrix(); inkCollector.Renderer.GetViewTransform(ref originalMatrix); Strokes strokesToDry = inkCollector.Ink.Strokes; Point p = viewportLocation; using (Matrix m = new Matrix()) { using (Graphics g = CreateGraphics()) { inkCollector.Renderer.PixelToInkSpace(g, ref p); g.Dispose(); } m.Translate(p.X, p.Y); inkCollector.Renderer.SetViewTransform(m); } LinkedList <ManagedBitmap> mapsToDry = new LinkedList <ManagedBitmap>(); foreach (Stroke s in strokesToDry) { for (int pi = 0; pi < s.GetPoints().Length; pi++) { Point sp2 = s.GetPoint(pi); using (Graphics g = CreateGraphics()) { inkCollector.Renderer.InkSpaceToPixel(g, ref sp2); } ManagedBitmap mbmp; if (!managedMaps.ContainsKey(new Point(getGridX(sp2.X), getGridY(sp2.Y)))) { mbmp = new ManagedBitmap(getGridX(sp2.X) * mapsWidth, getGridY(sp2.Y) * mapsHeight, new Bitmap(mapsWidth, mapsHeight)); mbmp.g.DrawRectangle(Pens.LightGray, 0, 0, mapsWidth - 1, mapsHeight - 1); mbmp.g.DrawString("(" + getGridX(sp2.X) + ", " + getGridY(sp2.Y) + ")", this.Font, Brushes.Black, 10f, 10f); managedMaps.Add(new Point(getGridX(sp2.X), getGridY(sp2.Y)), mbmp); } else { mbmp = managedMaps[new Point(getGridX(sp2.X), getGridY(sp2.Y))]; } if (!mapsToDry.Contains(mbmp)) { mapsToDry.AddLast(mbmp); } } } foreach (ManagedBitmap mbmp in mapsToDry) { //Point gridLocation = managedMaps.FirstOrDefault(x => x.Value == mbmp).Key; //System.Diagnostics.Debug.WriteLine(gridLocation.ToString()); Point mbmpP1 = new Point(mbmp.x + viewportLocation.X, mbmp.y + viewportLocation.Y); using (Graphics g = CreateGraphics()) { inkCollector.Renderer.PixelToInkSpace(g, ref mbmpP1); } Matrix m, m2; m = m2 = new Matrix(); inkCollector.Renderer.GetViewTransform(ref m); inkCollector.Renderer.GetViewTransform(ref m2); m.Translate(-mbmpP1.X, -mbmpP1.Y); inkCollector.Renderer.SetViewTransform(m); inkCollector.Renderer.Draw(mbmp.map, strokesToDry); inkCollector.Renderer.SetViewTransform(m2); this.drawingArea.CreateGraphics().DrawImage(mbmp.map, mbmp.x - viewportLocation.X, mbmp.y - viewportLocation.Y); mbmp.map.MakeTransparent(this.drawingArea.BackColor); } inkCollector.Renderer.SetViewTransform(originalMatrix); e.Cancel = true; if (strokesToDry.Count != 0) { inkCollector.Ink.DeleteStrokes(strokesToDry); } return(0); }); }
private void ListBoxFileNamesSelectedIndexChanged(object sender, EventArgs e) { checkBoxItalic.Checked = _italics[listBoxFileNames.SelectedIndex]; string name = listBoxFileNames.Items[listBoxFileNames.SelectedIndex].ToString(); string databaseName = _directoryPath + "Images.db"; Bitmap bmp = null; labelExpandCount.Text = string.Empty; labelImageInfo.Text = string.Empty; if (_binOcrDb != null) { var bob = GetSelectedBinOcrBitmap(); if (bob != null) { bmp = bob.ToOldBitmap(); labelImageInfo.Text = string.Format("Top:{0}, {1} colored pixels of {2}", bob.Y, bob.NumberOfColoredPixels, (bob.Width * bob.Height)); //bool italicI; //var isI = bob.IsLowercaseI(out italicI); //labelImageInfo.Text = string.Format("T:{0} j{1} :{2} i{3}{4} '{5} #{6}/{7}", bob.Y, bob.IsLowercaseJ(), bob.IsColon(), isI, italicI ? "i" : "", bob.IsApostrophe(), bob.NumberOfColoredPixels, (bob.Width * bob.Height)); if (bob.ExpandCount > 0) { labelExpandCount.Text = string.Format("Expand count: {0}", bob.ExpandCount); } } } else if (File.Exists(databaseName)) { using (var f = new FileStream(databaseName, FileMode.Open)) { if (name.Contains(']')) name = name.Substring(name.IndexOf(']') + 1).Trim(); f.Position = Convert.ToInt64(name); bmp = new ManagedBitmap(f).ToOldBitmap(); } } if (bmp == null) { bmp = new Bitmap(1, 1); labelImageInfo.Text = Configuration.Settings.Language.VobSubEditCharacters.ImageFileNotFound; } pictureBox1.Image = bmp; pictureBox1.Width = bmp.Width + 2; pictureBox1.Height = bmp.Height + 2; pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; var bmp2 = VobSubOcr.ResizeBitmap(bmp, bmp.Width * 2, bmp.Height * 2); pictureBox2.Image = bmp2; pictureBox2.Width = bmp2.Width + 2; pictureBox2.Height = bmp2.Height + 2; pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage; if (Additions != null && Additions.Count > 0) { if (_binOcrDb != null) { var bob = GetSelectedBinOcrBitmap(); foreach (var a in Additions) { if (bob != null && bob.Text != null && bob.Key == a.Name) { textBoxText.Text = a.Text; checkBoxItalic.Checked = a.Italic; break; } } } else { string target = GetSelectedFileName(); foreach (var a in Additions) { if (target.StartsWith(a.Name)) { textBoxText.Text = a.Text; break; } } } } }
private void ListBoxFileNamesSelectedIndexChanged(object sender, EventArgs e) { checkBoxItalic.Checked = _italics[listBoxFileNames.SelectedIndex]; string name = listBoxFileNames.Items[listBoxFileNames.SelectedIndex].ToString(); string databaseName = _directoryPath + "Images.db"; string posAsString = GetSelectedFileName(); Bitmap bmp = null; labelExpandCount.Text = string.Empty; if (_binOcrDb != null) { var bob = GetSelectedBinOcrBitmap(); if (bob != null) { bmp = bob.ToOldBitmap(); if (bob.ExpandCount > 0) { labelExpandCount.Text = string.Format("Expand count: {0}", bob.ExpandCount); } } } else if (File.Exists(databaseName)) { using (var f = new FileStream(databaseName, FileMode.Open)) { if (name.Contains(']')) name = name.Substring(name.IndexOf(']') + 1).Trim(); f.Position = Convert.ToInt64(name); bmp = new ManagedBitmap(f).ToOldBitmap(); } } if (bmp == null) { bmp = new Bitmap(1, 1); labelImageInfo.Text = Configuration.Settings.Language.VobSubEditCharacters.ImageFileNotFound; } pictureBox1.Image = bmp; pictureBox2.Width = bmp.Width * 2; pictureBox2.Height = bmp.Height * 2; pictureBox2.Image = bmp; if (Additions != null && Additions.Count > 0) { if (_binOcrDb != null) { var bob = GetSelectedBinOcrBitmap(); foreach (var a in Additions) { if (bob != null && bob.Text != null && bob.Key == a.Name) { textBoxText.Text = a.Text; checkBoxItalic.Checked = a.Italic; break; } } } else { string target = GetSelectedFileName(); foreach (var a in Additions) { if (target.StartsWith(a.Name)) { textBoxText.Text = a.Text; break; } } } } }
private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e) { labelImageInfo.Text = string.Empty; labelExpandCount.Text = string.Empty; if (listBoxInspectItems.SelectedIndex < 0) return; _selectedCompareNode = null; _selectedCompareBinaryOcrBitmap = null; pictureBoxInspectItem.Image = _imageSources[listBoxInspectItems.SelectedIndex]; pictureBoxCompareBitmap.Image = null; pictureBoxCompareBitmapDouble.Image = null; int index = (listBoxInspectItems.SelectedIndex); var match = _matches[index]; _selectedMatch = match; if (!string.IsNullOrEmpty(match.Name)) { if (_binOcrDb != null) { bool bobFound = false; foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImages) { if (match.Name == bob.Key) { textBoxText.Text = bob.Text; checkBoxItalic.Checked = bob.Italic; _selectedCompareBinaryOcrBitmap = bob; var bitmap = bob.ToOldBitmap(); pictureBoxCompareBitmap.Image = bitmap; pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2; pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2; pictureBoxCompareBitmapDouble.Image = bitmap; var matchBob = new BinaryOcrBitmap(new NikseBitmap(_imageSources[listBoxInspectItems.SelectedIndex])); if (matchBob.Hash == bob.Hash && matchBob.Width == bob.Width && matchBob.Height == bob.Height && matchBob.NumberOfColoredPixels == bob.NumberOfColoredPixels) { buttonAddBetterMatch.Enabled = false; // exact match } else { buttonAddBetterMatch.Enabled = true; } bobFound = true; break; } } if (!bobFound) { foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImagesExpanded) { if (match.Name == bob.Key) { textBoxText.Text = bob.Text; checkBoxItalic.Checked = bob.Italic; _selectedCompareBinaryOcrBitmap = bob; var bitmap = bob.ToOldBitmap(); pictureBoxCompareBitmap.Image = bitmap; pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2; pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2; pictureBoxCompareBitmapDouble.Image = bitmap; buttonAddBetterMatch.Enabled = false; // exact match labelExpandCount.Text = string.Format("Expand count: {0}", bob.ExpandCount); break; } } } } else { foreach (XmlNode node in ImageCompareDocument.DocumentElement.ChildNodes) { if (node.Attributes["Text"] != null && node.InnerText == match.Name) { string text = node.Attributes["Text"].InnerText; textBoxText.Text = text; checkBoxItalic.Checked = node.Attributes["Italic"] != null; string databaseName = Path.Combine(_directoryPath, "Images.db"); using (var f = new FileStream(databaseName, FileMode.Open)) { try { string name = node.InnerText; int pos = Convert.ToInt32(name); f.Position = pos; var mbmp = new ManagedBitmap(f); var bitmap = mbmp.ToOldBitmap(); pictureBoxCompareBitmap.Image = bitmap; pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2; pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2; pictureBoxCompareBitmapDouble.Image = bitmap; labelImageInfo.Text = string.Format(Configuration.Settings.Language.VobSubEditCharacters.Image + " - {0}x{1}", bitmap.Width, bitmap.Height); } catch (Exception exception) { labelImageInfo.Text = Configuration.Settings.Language.VobSubEditCharacters.Image; MessageBox.Show(exception.Message); } } _selectedCompareNode = node; break; } } } } buttonAddBetterMatch.Text = Configuration.Settings.Language.VobSubOcrCharacterInspect.AddBetterMatch; if (_selectedMatch.Text == Configuration.Settings.Language.VobSubOcr.NoMatch) { buttonUpdate.Enabled = false; buttonDelete.Enabled = false; buttonAddBetterMatch.Enabled = true; buttonAddBetterMatch.Text = Configuration.Settings.Language.VobSubOcrCharacterInspect.Add; textBoxText.Enabled = true; textBoxText.Text = string.Empty; checkBoxItalic.Enabled = true; } else if (_selectedCompareNode == null && _selectedCompareBinaryOcrBitmap == null) { buttonUpdate.Enabled = false; buttonDelete.Enabled = false; buttonAddBetterMatch.Enabled = true; textBoxText.Enabled = true; textBoxText.Text = string.Empty; checkBoxItalic.Enabled = false; } else { buttonUpdate.Enabled = true; buttonDelete.Enabled = true; if (_selectedCompareNode != null) buttonAddBetterMatch.Enabled = true; textBoxText.Enabled = true; checkBoxItalic.Enabled = true; } }
private static int CalculateNumberOfForegroundColors(ManagedBitmap managedBitmap) { int count = 0; for (int y = 0; y < managedBitmap.Height; y++) { for (int x = 0; x < managedBitmap.Width; x++) { Color c = managedBitmap.GetPixel(x, y); if (c.A > 100 && c.R + c.G + c.B > 200) count++; } } return count; }
internal static int IsBitmapsAlike(NikseBitmap bmp1, ManagedBitmap bmp2) { int different = 0; int maxDiff = bmp1.Width * bmp1.Height / 5; for (int x = 1; x < bmp1.Width; x++) { for (int y = 1; y < bmp1.Height; y++) { if (!IsColorClose(bmp1.GetPixel(x, y), bmp2.GetPixel(x, y), 20)) different++; } if (different > maxDiff) return different + 10; } return different; }
private static WriteableBitmap _Get(string name) { lock(_lock) { if (name != _loadedName) { ManagedBitmap managedBitmap; using (var stream = new System.IO.FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); var frame = decoder.Frames[0]; managedBitmap = new ManagedBitmap(frame); } using(var lockedBitmap = managedBitmap.Lock()) { int height = lockedBitmap.Height; int width = lockedBitmap.Width; for(int y=0; y<height; y++) { for(int x=0; x<width; x++) { var color = lockedBitmap[x, y]; if (color.Red < 10 && color.Green < 10 && color.Blue < 10) lockedBitmap[x, y] = new Argb(); } } } _bitmap = managedBitmap.WriteableBitmap; _loadedName = name; } return _bitmap; } }
/// <summary> /// Initializes a new instance of the <see cref="CompareItem"/> class. /// </summary> /// <param name="bmp"> /// The bmp. /// </param> /// <param name="name"> /// The name. /// </param> /// <param name="isItalic"> /// The is italic. /// </param> /// <param name="expandCount"> /// The expand count. /// </param> /// <param name="text"> /// The text. /// </param> public CompareItem(ManagedBitmap bmp, string name, bool isItalic, int expandCount, string text) { this.Bitmap = bmp; this.Name = name; this.Italic = isItalic; this.ExpandCount = expandCount; this.NumberOfForegroundColors = -1; this.Text = text; }