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);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM