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:
- class Screenshot
- {
- public static bool saveToClipboard = true;
- public static void CaptureImage(bool showCursor, Size curSize, Point curPos, Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath, string extension)
- {
- using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
- {
- using (Graphics g = Graphics.FromImage(bitmap))
- {
- g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
- if (showCursor)
- {
- Rectangle cursorBounds = new Rectangle(curPos, curSize);
- Cursors.Default.Draw(g, cursorBounds);
- }
- }
- using (MemoryStream ms = new MemoryStream())
- {
- Image img = (Image)bitmap;
- img.Save(ms, ImageFormat.Jpeg);
- Bitmap bm = new Bitmap(ms);
- Clipboard.SetImage(bm);
- if (!File.Exists(@"C:\Users\Cowboy\Desktop\Images\mstemp.jpg"))
- {
- img.Save(@"C:\Users\Cowboy\Desktop\Images\mstemp.jpg");
- }
- else
- {
- File.Delete(@"C:\Users\Cowboy\Desktop\Images\mstemp.jpg");
- img.Save(@"C:\Users\Cowboy\Desktop\Images\mstemp.jpg");
- }
- ms.Dispose();
- }
- }
- }
- }
- }
IronOCR and tesseract will not read the images.
- var ocr = new AutoOcr();
- var result = ocr.Read(@"C:\Users\Cowboy\Desktop\mstemp.jpg");
- 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.
Maen BadwanPosted Feb 14, 2018, 8:52 AM
After saving the image into JPEG format, try to open it using one of the commonly known viewers, such as MS Paint. If the image loads correctly, try to manually save it to PNG format and pass it to the IronOCR or the Tesseract toolkits and see if this fixes the OCR recognition issue.
2. Before saving the image data into MemoryStream, try to seek the stream to the beginning (origin) using the following method: