We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Unable to cast object of type 'Aspose.Drawing.Imaging.Metafile' to type 'Aspose.Drawing.Bitmap'

I try to use OCR on a JPG file, which is inside a MemoryStream which gives me above exception.

var recognitionResult = recognitionEngine.RecognizeImage(memoryStream, recognitionSettings);

Different recognitionSettings make no difference.

I can workaround the exception if I load the JPG and save it to a Bitmap first:
using (Image image = Image.Load(_filestream))
using (var bitmapStream = new MemoryStream())
var bitmapOptions = new BmpOptions();
image.Save(bitmapStream, bitmapOptions);

Your documentation says it can OCR an JPG directly. I’m using Aspose.OCR for .NET 22.9.


Is it occurring with every JPG file that you are trying to process or with some specific one? Would you please also try other formats like PNG and let us know if issue still persists?

I tried to reproduce the behaviour, but can’t at the moment.
I conclude that this means, that this occurs only by a specific JPG image.

I will try to figure this out and come back.

Seems to be a licensing issue.
I initiated an Aspose.Imaging.License instead of an Aspose.OCR.License which seems to create this issue.


Please feel free to create a new topic in case you need more information about the API. We will assist you accordingly.

In the meantime I could reproduce this: it happens when your position within the stream is not at the start of the file. Most Aspose methods move the position but this one not.

It’s no problem for me, because I know this now.


Thanks for the feedback. Please feel free to let us know if you face any other issues.