Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random exceptions when calling UWP APIs on .NET 5 #46148

Closed
a11cf0 opened this issue Dec 16, 2020 · 2 comments
Closed

Random exceptions when calling UWP APIs on .NET 5 #46148

a11cf0 opened this issue Dec 16, 2020 · 2 comments
Labels
untriaged New issue has not been triaged by the area owner

Comments

@a11cf0
Copy link

a11cf0 commented Dec 16, 2020

Description

I'm trying to call Windows 10 UWP OCR APIs from a .NET 5 Windows Forms application. The code which i'm using was perfectly working on .NET Core 3.1 but on .NET 5 it's extremely unstable. For some reason the code throws either ObjectDisposedException or sometimes InvalidCastException. this usually happens on the call to GetSoftwareBitmapAsync() but sometimes right on the first await. Also, hen wrapped in a method, it usually works on the first run but on subsequent runs it fails as above. Here's a full code sample tested on both .NET Core 3.1 and .NET 5 with the above results.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;

namespace Supergovno
{
    static class Program
    {
        static async Task<int> Main()
        {
            Bitmap Screen = new Bitmap(500, 500);
            Graphics g = Graphics.FromImage(Screen);
            g.CopyFromScreen(1, 1, 00, 0, Screen.Size);
            Bitmap S = new Bitmap(Screen, new Size(500, 500));
            var engine = Windows.Media.Ocr.OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language("en"));
            using MemoryStream memoryStream = new();
            S.Save(memoryStream, ImageFormat.Bmp);
            var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(memoryStream.AsRandomAccessStream());
            var softwareBitmap = await decoder.GetSoftwareBitmapAsync();
            var ocrResult = await engine.RecognizeAsync(softwareBitmap);
            Console.WriteLine(ocrResult.Text);
            Console.WriteLine("OK!");
            Console.ReadLine();
            return 0;
        }
    }
}

Configuration

I'm using .NET 5.0.101 on Windows 10 X64 10.0.19042.

Regression?

Yes. Everything is working on .NET Core 3.1.

Other information

@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Dec 16, 2020
@jkotas
Copy link
Member

jkotas commented Dec 16, 2020

Transferred to microsoft/CsWinRT#640

@jkotas jkotas closed this as completed Dec 16, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants