Branden

Branden

  • NA
  • 9
  • 7.2k

OCR not reading images created from MemoryStream or Bitmap

Feb 9 2018 4:58 PM
I am using the IronOCR to read an image to text, I ave also tried Tesseract and Tessnet to no avail. However, it is not reading the file created from my MemoryStream() or just Bitmap.Save in general.
 
The code below is funcitonal: 
 
  1.  class Screenshot  
  2.     {  
  3.         public static bool saveToClipboard = true;  
  4.   
  5.         public static void CaptureImage(bool showCursor, Size curSize, Point curPos, Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath, string extension)  
  6.         {  
  7.   
  8.             using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))  
  9.             {  
  10.   
  11.                 using (Graphics g = Graphics.FromImage(bitmap))  
  12.                 {  
  13.   
  14.                     g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);  
  15.   
  16.                     if (showCursor)  
  17.                     {  
  18.                         Rectangle cursorBounds = new Rectangle(curPos, curSize);  
  19.                         Cursors.Default.Draw(g, cursorBounds);  
  20.                     }  
  21.   
  22.                       
  23.   
  24.                 }  
  25.   
  26.                 using (MemoryStream ms = new MemoryStream())  
  27.                 {  
  28.                       
  29.                     Image img = (Image)bitmap;  
  30.   
  31.                     img.Save(ms, ImageFormat.Jpeg);  
  32.                     Bitmap bm = new Bitmap(ms);  
  33.                     Clipboard.SetImage(bm);  
  34.   
  35.                     if (!File.Exists(@"C:\Users\Cowboy\Desktop\Images\mstemp.jpg"))  
  36.                     {  
  37.                         img.Save(@"C:\Users\Cowboy\Desktop\Images\mstemp.jpg");  
  38.                           
  39.                     }  
  40.                     else  
  41.                     {  
  42.                         File.Delete(@"C:\Users\Cowboy\Desktop\Images\mstemp.jpg");  
  43.                         img.Save(@"C:\Users\Cowboy\Desktop\Images\mstemp.jpg");  
  44.                           
  45.                     }  
  46.   
  47.                     ms.Dispose();  
  48.                 }  
  49.   
  50.             }  
  51.   
  52.         }  
  53.   
  54.     }  
  55. }  
 
 
IronOCR and tesseract will not read the images.

  1.             var ocr = new AutoOcr();  
  2.             var result = ocr.Read(@"C:\Users\Cowboy\Desktop\mstemp.jpg");  
  3.             tbOCR.Text = result.GetType().ToString();  

 
However, if IronOCR is able to read screenshot created by other applications like green shot, light shot, and print screen (a MS built in function), then the code I am using is in fact excluding some important raw data that would otherwise prevent the IronOCR functionality from doing its job. Nothing is wrong with IronOCR, its how the raw data stream/bitmap of my images are preventing the programs like IronOCR or Tesseract from reading my screenshots.
 

Answers (1)