I am writing a code to call recognize function of Aspose ocr .net code from C++ application using CLI implementation.
When i run as console application recognize function works fine, But when i call the same function from c++ code through CLI wrapper it is throwing me the below errors.
2/27/2025 5:35:45 PM: Aspose_Export()::Error during OCR recognition: System.Memory
2/27/2025 5:35:45 PM: Aspose_Export()::Stack Trace: at System.ReadOnlySpan1..ctor(T[] array) at .(Single[] , Byte[] ) at .(Byte[,][] ) at .(Byte[,][] ) at .(Byte[,][] , , Single& ) at .(Byte[,][] , String , RecognitionSettings , ) at .(ImageData , RecognitionSettings , , Action
4 )
at .(ImageData , RecognitionSettings , , Action4 ) at .(OcrInput , RecognitionSettings , , Action
4 , CancellationToken )
at .(OcrInput , RecognitionSettings , , Action`4 )
at Aspose.OCR.AsposeOcr.Recognize(OcrInput images, RecognitionSettings settings)
at AsposeOCRLib.AsposeOCRApi.Aspose_Export(String exportFileName, Int32 format, IntPtr exportParams) in C:\Current\AsposeOCRLib\AsposeOCRApi.cs:line 389
2/27/2025 5:35:45 PM: Aspose_Export()::Error during export: Could not load file or assembly ‘System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Below are my code snippets.
.cs file:
public bool Aspose_Export(string exportFileName, int format, IntPtr exportParams)
{
try
{
// Initialize the OCR engine
//AsposeOcr ocrEngine = new AsposeOcr();
string czImageFilePath = “C:\Aspose_DOC\JPG\GIF_SinglePageText_OCR_1.gif”;
MessageBox.Show("Path: " + czImageFilePath);
// Check if the file exists
if (!File.Exists(czImageFilePath))
{
throw new FileNotFoundException("The specified image file does not exist.", czImageFilePath);
}
MessageBox.Show("File is exist : " + czImageFilePath);
// Create an instance of OcrInput with the correct parameters
OcrInput input = new OcrInput(InputType.SingleImage);
input.Add("C:\\Aspose_DOC\\JPG\\JPG_SinglePage_3.jpg");
MessageBox.Show("input added");
// Log the input details
//Aspose.OCR.AsposeOcr recognitionEngine = new Aspose.OCR.AsposeOcr();
//Log($"Aspose_Export()::Processing file: {czImageFilePath}");
if(ocrEngine == null)
{
MessageBox.Show("OCR Engine is null");
ocrEngine = new AsposeOcr();
}
else
{
MessageBox.Show("OCR Engine is not null");
}
// Set recognition language (optional)
RecognitionSettings recognitionSettings = new RecognitionSettings();
//// Perform OCR recognition
List<RecognitionResult> results = null;
try
{
results = ocrEngine.Recognize(input, recognitionSettings);
//results = ocrEngine.Recognize(input, new RecognitionSettings());
MessageBox.Show("OCR recognition completed");
}
catch (Exception ex)
{
MessageBox.Show("Error during OCR recognition");
Log($"Aspose_Export()::Error during OCR recognition: {ex.Source}");
Log($"Aspose_Export()::Stack Trace: {ex.StackTrace}");
throw;
}
MessageBox.Show("Recognize the input");
if (results == null || results.Count == 0)
{
throw new InvalidOperationException("OCR recognition failed to produce results.");
}
// Determine the export format and save the result accordingly
switch (format)
{
case 0:
MessageBox.Show("Switch RTF");
SaveAsRtf(exportFileName, results[0].RecognitionText);
MessageBox.Show("Exit RTF switch");
break;
case 1:
//SaveAsPdf(exportFileName, results[0].RecognitionText);
break;
default:
throw new NotSupportedException($"Format {format} is not supported.");
}
MessageBox.Show("Returning TRUe");
return true;
}
catch (FileNotFoundException ex)
{
Log($"Aspose_Export()::File not found: {ex.Message}");
return false;
}
catch (UnauthorizedAccessException ex)
{
Log($"Aspose_Export()::Access denied: {ex.Message}");
return false;
}
catch (Exception ex)
{
Log($"Aspose_Export()::Error during export: {ex.Message}");
return false;
}
}
none of the parameters sent from CLI is not used, as you can see in the above code we hardcoded the input image file path also the export file path. I have not added the CLI wrapper function, please let me know if required.
I tried to downgrade the System.Runtime.CompilerServices.Unsafe from 6.0.0 to 4.5.3 version, still error is exists.