这是我的代码: 我在这里用这段代码填充了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"
答案 0 :(得分:1)
有时在设置StreamSource之前将流位置设置为0可以解决此问题:
inputImageStream.Position = 0;