C# BitmapImage.EndInit()抛出';System.NotSupportedException';

C# BitmapImage.EndInit()抛出';System.NotSupportedException';,c#,memorystream,bitmapimage,C#,Memorystream,Bitmapimage,这是我的密码: 我在这里用这个代码填充MemoryStream private void treeView_MouseDoubleClick(object sender, MouseButtonEventArgs e) { try { var a = treeView.SelectedItem as TreeViewItem; if (a.Header.ToString().EndsWith(".jpg")

这是我的密码: 我在这里用这个代码填充MemoryStream

private void treeView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        try
        {
            var a = treeView.SelectedItem as TreeViewItem;

            if (a.Header.ToString().EndsWith(".jpg"))
            {
                Bitmap temp = new Bitmap(a.Tag.ToString());

                try
                {
                    OriginalImage.Dispose();  
                }
                catch { };
                OriginalImage = new MemoryStream(); 

                temp.Save(OriginalImage, ImageFormat.Bmp);                    
                Uri path = new Uri(a.Tag.ToString());
                actualImage.Source = new BitmapImage(path);

            }
        }
        catch { };
    }
就在那个块之后,这应该会发生,但是在“bitmap.EndInit()”上,我得到了异常“System.NotSupportedException”

 var bitmap = new BitmapImage();                
 bitmap.BeginInit();
 bitmap.StreamSource = OriginalImage;
 bitmap.CacheOption = BitmapCacheOption.OnLoad;
 bitmap.EndInit();
 bitmap.Freeze();    
 actualImage.Source = bitmap;
有人知道是什么引起的吗?假设这些方法在System.Windows.Controls.image中加载图像,第二个块是在编辑图片之前将加载的图像返回到其默认值

NotSupportedException.StackTrace是:

"at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)\r\n   at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)\r\n   at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()\r\n   at System.Windows.Media.Imaging.BitmapImage.EndInit()\r\n   at WpfApplication2.MainWindow.resetBtn_Click(Object sender, RoutedEventArgs e) in c:\\Users\\Albert Sato Damas\\Desktop\\ImageEditing - Copy - Copy\\WpfApplication2\\MainWindow.xaml.cs:line 270"

有时,在设置StreamSource之前将stream position设置为0可以解决此问题:

inputImageStream.Position = 0;

异常的堆栈跟踪是什么?所以我们可以看到什么是不快乐的…@MarcGravel我怎么能检查?我只处理过几次异常。在调试器中,您应该能够查看异常详细信息,然后单击有帮助的
.StackTrace
@marcGravel?如果您使用流两次创建位图图像,这一点很重要。