0

这是我的代码:我在这里用这段代码填充 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"
4

1 回答 1

1

有时在设置 StreamSource 之前设置流位置为 0 可以解决这个问题:

inputImageStream.Position = 0;
于 2016-09-24T19:23:29.043 回答