后台代码:
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "BMP|*.bmp|PNG|*.png|JPG|*.jpg";
if (save.ShowDialog().Value)
{
if (SaveImg(save.FileName))
{
MessageBox.Show("保存成功!");
}
}
}
private bool SaveImg(string path)
{
try
{
FileStream fs = new FileStream(path, FileMode.Create);
RenderTargetBitmap bmp = new RenderTargetBitmap((int)ic.ActualWidth, //ic是控件的名字
(int)ic.ActualHeight, 1 / 96, 1 / 96, PixelFormats.Pbgra32);
bmp.Render(ic);
BitmapEncoder encoder = new TiffBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(fs);
fs.Close();
return true;
}
catch (Exception)
{
return false;
}
}
引用:
*****using Microsoft.Win32;
******using System.IO;
网友评论