FREngine.IImage.GetBitmap returns COMException (0x80020008): Bad variable type

  • 122 Views
  • Last Post 06 March 2017
Ashley posted this 02 March 2017

Scenario:

  • We are using v11.1.19.48 of the FREngine
  • The engine is invoked from a server-side process (.NET 4.0)
  • OCR works fine with EngineLoader. We have code in production using it.
  • BUT the EngineLoader only allows us to extract OCR results in a single-thread. When I try and run multiple threads, I get an AccessViolationException. According to the Abbyy documentation, you need to use OutprocLoader for extracting results in a multi-threaded application.
  • So I am trying to use OutprocLoader instead of EngineLoader
  • The OCR process works fine with OutprocLoader. But after the OCR has been performed, we run the following code to try and extract a bitmap of the document pages:

    private System.Drawing.Image GetImageFromPage(IFRPage frPage)
    {
        try
        {
            IImageDocument imageDocument = frPage.ImageDocument;
            Image image = null;
    
            switch (imageDocument.ImageColorType)
            {
                case ImageColorTypeEnum.ICT_BlackWhite:
                    image = imageDocument.BlackWhiteImage;
                    break;
                case ImageColorTypeEnum.ICT_Gray:
                case ImageColorTypeEnum.ICT_Color:
                    image = imageDocument.ColorImage;
                    break;
            }
    
            // Error coming from this line:
            var hBitmap = image.GetBitmap(_ocrEngine.CreateImageModification());
    
            System.Drawing.Image newImage = System.Drawing.Image.FromHbitmap(hBitmap.Handle);
            hBitmap.CloseHandle();
    
            return newImage;
        }
        catch (Exception ex)
        {
            // custom error logging
        }
    }
    

The code above results in the following exception:

20170302114127: System.Runtime.InteropServices.COMException (0x80020008): Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE))
   at FREngine.IImage.GetBitmap(ImageModification Modification)
   at BZAConsoleApp.FileProcessing.OcrFileProcessor.GetImageFromPage(IFRPage frPage) in F:\dev\BizAnalyser\BizAnalyser\BZAConsoleApp\FileProcessing\OcrFileProcessor.cs:line 238

The line of code causing the exception is this one:

var hBitmap = image.GetBitmap(_ocrEngine.CreateImageModification());

The same error occurs with this code:

var hBitmap = image.GetBitmap(null);

The identical code works successfully when we use the EngineLoader - that code is currently in Production.

Can someone please assist? We are close to procuring a new client, but we need to speed up our extraction of the OCR results, so are urgently looking in to how we can perform some of that code in multiple threads.

Thanks

Oksana Serdyuk posted this 06 March 2017

Hi, please sorry for the delay.

When the Engine object is created using the OutprocLoader object, the Image::GetBitmap method does not work. Thus, the only way to get the image is to use the Image::WriteToFile method.

Also you can slightly speed up the program execution time, replacing

switch (imageDocument.ImageColorType)
{
   case ImageColorTypeEnum.ICT_BlackWhite:
      image = imageDocument.BlackWhiteImage;
      break;
   case ImageColorTypeEnum.ICT_Gray:
   case ImageColorTypeEnum.ICT_Color:
      image = imageDocument.ColorImage;
      break;
}

at

image = imageDocument.ColorImage;

Even if the original image is gray or black-and-white, it will be contained in the ImageDocument::ColorImage property.

Close