private void BtnLaunchResultsViewer_Click(object sender, RoutedEventArgs e) { Main.SetStatus(0, ""); if (System.IO.File.Exists(ViewModel.SampleImage) == false) { Main.SetStatus(0, "Please specify a sample image file"); return; } OcrDemo.UI.Lib.OcrResults win = new UI.Lib.OcrResults(); Contracts.entity.TextExtractionResults lastOcrResults = new Contracts.entity.TextExtractionResults { Blocks = new Contracts.entity.TextBlock[] { new Contracts.entity.TextBlock { Text = "hello", X1 = 100, X2 = 150, Y1 = 100, Y2 = 150 }, new Contracts.entity.TextBlock { Text = "hello 2", X1 = 200, X2 = 250, Y1 = 200, Y2 = 250 } } }; var bytes = System.IO.File.ReadAllBytes(ViewModel.SampleImage); win.RenderImageWithOverlay(bytes, lastOcrResults); win.Show(); ViewModel.SaveSettings(); }
private void RenderImage(byte[] raw, TextExtractionResults lastOcrResults) { Contracts.entity.TextExtractionResults ocr = null; if (lastOcrResults == null) { ctlStatusPanel0.Text = $"Found {lastOcrResults.Blocks.Length} text objects"; //Create an empty object if none was specified ocr = new Contracts.entity.TextExtractionResults { Blocks = new Contracts.entity.TextBlock[] { } }; } else { ocr = lastOcrResults; } System.Drawing.Pen penBlock = new System.Drawing.Pen(System.Drawing.Color.Black); System.Drawing.Pen penSentence = new System.Drawing.Pen(System.Drawing.Color.Orange, 3); System.Drawing.Pen penPara = new System.Drawing.Pen(System.Drawing.Color.Blue, 3); System.Drawing.Brush b = new System.Drawing.SolidBrush( System.Drawing.Color.FromArgb(100, System.Drawing.Color.Yellow)); using (var memStm = new System.IO.MemoryStream(raw)) { var imge = System.Drawing.Image.FromStream(memStm); _picBox.Image = imge; using (var g = System.Drawing.Graphics.FromImage(imge)) { foreach (var box in ocr.Blocks) { //var pts = new System.Drawing.Point[] //{ // new System.Drawing.Point((int)box.X1,(int)box.Y1), // new System.Drawing.Point((int)box.X2,(int)box.Y1), // new System.Drawing.Point((int)box.X2,(int)box.Y2), // new System.Drawing.Point((int)box.X1,(int)box.Y2), // new System.Drawing.Point((int)box.X1,(int)box.Y1) //}; //g.DrawLines(pen,pts); int xUpperLeft = (int)Math.Min(box.X1, box.X2); int yUpperLeft = (int)Math.Min(box.Y1, box.Y2); float width = (float)Math.Abs(box.X1 - box.X2); float ht = (float)Math.Abs(box.Y1 - box.Y2); g.DrawRectangle(penBlock, xUpperLeft, yUpperLeft, width, ht); g.FillRectangle(b, xUpperLeft, yUpperLeft, width, ht); } foreach (var sentence in ocr.Sentences) { g.DrawLine( penSentence, sentence.Rectangle.X, sentence.Rectangle.Bottom, sentence.Rectangle.Right, sentence.Rectangle.Bottom); } foreach (var para in ocr.Paragraphs) { g.DrawRectangle( penPara, para.Rectangle.Left, para.Rectangle.Top, para.Rectangle.Width, para.Rectangle.Height); } } } }
public void RenderImageWithOverlay(byte[] raw, Contracts.entity.TextExtractionResults lastOcrResults) { RenderImage(raw, lastOcrResults); RenderJson(lastOcrResults); }