wpf 获取image控件的图片并保存

XMAL代码如下:

<Image Name="ImageToSave" Source="Images/pic_bg.png" Grid.RowSpan="3" Grid.ColumnSpan="3"  ></Image>

C#代码如下:

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Image Files (*.bmp, *.png, *.jpg)|*.bmp;*.png;*.jpg | All Files | *.*";
sfd.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
if (sfd.ShowDialog() == true)
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)this.ImageToSave.Source));
using (FileStream stream = new FileStream(sfd.FileName, FileMode.Create))
encoder.Save(stream);
}

posted @ 2017-11-15 19:49  liu_xh  阅读(5381)  评论(0编辑  收藏  举报