BitmapImage.EndInit() throwing 'System.NotSupportedExcep
本文关键字:System NotSupportedExcep throwing EndInit BitmapImage | 更新日期: 2023-09-27 18:37:17
这是我的代码:我在这里用这段代码填充 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 { };
}
就在那个块之后,这应该会发生,但在"位图"上。EndInit()" 我得到异常'系统.不支持异常"。
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 之前将流位置设置为 0 可以解决此问题:
inputImageStream.Position = 0;