C# 以流形式从PC加载图像

C# 以流形式从PC加载图像,c#,uwp,microsoft-cognitive,C#,Uwp,Microsoft Cognitive,我正在尝试从我的电脑加载一张图片作为原始图像,以便与Microsoft认知服务情感(UWP)一起使用。 下面是我的一段代码: //Chose Image from PC private async void chosefile_Click(object sender, RoutedEventArgs e) { //Open Dialog FileOpenPicker open = new FileOpenPicker();

我正在尝试从我的电脑加载一张图片作为原始图像,以便与Microsoft认知服务情感(UWP)一起使用。 下面是我的一段代码:

        //Chose Image from PC
    private async void chosefile_Click(object sender, RoutedEventArgs e)
    {


        //Open Dialog
        FileOpenPicker open = new FileOpenPicker();
        open.ViewMode = PickerViewMode.Thumbnail;
        open.SuggestedStartLocation = PickerLocationId.Desktop;
        open.FileTypeFilter.Add(".jpg");
        open.FileTypeFilter.Add(".jpeg");
        open.FileTypeFilter.Add(".gif");
        open.FileTypeFilter.Add(".png");
        file = await open.PickSingleFileAsync();


        if (file != null)
        {//imagestream is declared as IRandomAccessStream.

            imagestream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
            var image = new BitmapImage();
            image.SetSource(imagestream);
            imageView.Source = image;
        }
        else
        {
            //  
        }
    }
上面的部分工作正常,它从pc(对话框)中选择一张照片并在图像框中显示

    private async void analyse_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            emotionResult = await emotionServiceClient.RecognizeAsync(imagestream.AsStream());
        }
        catch
        {
            output.Text = "something is wrong in stream";
        }

        try { 
            if(emotionResult!= null)
            {
                Scores score = emotionResult[0].Scores;
                output.Text = "Your emotions are: \n" +
                    "Happiness: " + score.Happiness + "\n" +
                    "Sadness: " + score.Sadness;
            }
        }
        catch
        {
         output.Text = "Something went wrong";
        }
    }
我认为错误是由imagestream.AsStream()引起的 imagestream被声明为irandomaccesstream

有人可以告诉我如何修复该部分,如果错误实际上是由于没有正确加载图像

编辑:
还有更好的方法,而不是使用stream向emotionServiceClient传递保存的文件而不是流吗?

为什么不使用他们的示例,而不是尝试将文件保存在内存中,为什么不保留路径,然后使用路径在此时读取流

例如:

using (Stream imageFileStream = File.OpenRead(imageFilePath))
                {
                    //
                    // Detect the emotions in the URL
                    //
                    emotionResult = await emotionServiceClient.RecognizeAsync(imageFileStream);
                    return emotionResult;
                }

因此,您将捕获imageFilePath作为打开文件对话框的结果。

为什么不使用他们的示例,而不是尝试将文件保存在内存中,为什么不保存一个路径,然后使用该路径在此时读取流

例如:

using (Stream imageFileStream = File.OpenRead(imageFilePath))
                {
                    //
                    // Detect the emotions in the URL
                    //
                    emotionResult = await emotionServiceClient.RecognizeAsync(imageFileStream);
                    return emotionResult;
                }

因此,您将捕获imageFilePath作为“打开文件”对话框的结果。

您的问题是,通过创建
位图图像
,您已经提升了流位置,因此,在调用
emotionServiceClient.RecognitizeAsync时,您的读取位置已在末尾。所以你需要“倒带”:

var stream = imagestream.AsStreamForRead();
stream.Position = 0;
emotionResult = await emotionServiceClient.RecognizeAsync(stream);

您的问题是,通过创建
位图图像
,您已经提升了流位置,因此在调用
emotionServiceClient.RecognitizeAsync
时,您的读取位置位于末尾。所以你需要“倒带”:

var stream = imagestream.AsStreamForRead();
stream.Position = 0;
emotionResult = await emotionServiceClient.RecognizeAsync(stream);

你好我尝试了这个建议,并得到以下错误:System.InvalidOperationException:不应在UI线程上执行同步操作。考虑在Task.Run中包装此方法。位于System.IO.FileStream.Init的System.IO.WinRTFileSystem.EnsureBackgroundThread()处的System.IO.WinRTFileSystem.Open(字符串完整路径、文件模式、文件访问访问、文件共享、Int32 bufferSize、文件选项、文件流父级)(字符串路径、文件模式、文件访问权限、文件共享共享、Int32 bufferSize、文件选项选项)在System.IO.FileStream..ctor(字符串路径、文件模式、文件访问权限、文件共享)在System.IO.File.OpenRead(字符串路径)在EmoRec.MainPage.d_u8.MoveNext()您好!我尝试了这个建议并得到以下错误:Stult.ValualOptualExcExct:不应该在UI线程上执行同步操作。考虑在Task.Run.Type .Io.WiRTFielSealSt.EnSureBeaBead THead()中在System.IO.WinRTFileSystem.Open中包装此方法。System.IO.FileStream.Init(System.IO.FileStream.Init(字符串路径、FileMode模式、FileAccess访问、FileShare共享、Int32 bufferSize、FileOptions选项、FileStream父级)、System.IO.FileStream.Init(字符串路径、FileMode模式、FileAccess访问、FileShare共享、Int32 bufferSize、FileOptions选项)(字符串路径、文件模式、文件访问访问、文件共享)在EmoRec.MainPage.d_u8.MoveNext()的System.IO.File.OpenRead(字符串路径)中捕获的异常消息是什么?捕获的异常消息是什么?