保存图片控件上的图片到本地 出现错误:无法将类型为“System.Windows.Media.Imaging.BitmapFrameDecode”的对象强制转换为类型“System.Windows.Media.Imaging.BitmapImage”。
try { System.Windows.Controls.MenuItem menuitem = sender as System.Windows.Controls.MenuItem; BitmapImage bitmap = (BitmapImage)imgPhoto.Source; (错误地方) JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); FileStream fileStream = new FileStream(saveFileName, FileMode.Create, FileAccess.ReadWrite); encoder.Save(fileStream); fileStream.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString());
}
修改成:
BitmapSource bitmap = (BitmapSource)imgPhoto.Source;
即可解决问题