private void ExportBtn_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "jpg文件|*.jpg|png文件|*.png|bmp文件|*.bmp|所有文件|*.*";
saveFileDialog.RestoreDirectory = true;
saveFileDialog.ShowDialog();
string path = saveFileDialog.FileName;
if (path == "")
{
return;
}
var image = (this.imagListBox.SelectedItem as PictureInfo).PicturePath;//图片的文件路径
string pictureName = saveFileDialog.FileName;
using (MemoryStream mem = new MemoryStream())
{
//这句很重要,不然不能正确保存图片或出错(关键就这一句)
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(image);
string extension = System.IO.Path.GetExtension(path);//扩展名
//保存到磁盘文件
if (extension == ".jpg")
{
bmp.Save(@pictureName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else if (extension == ".png")
{
bmp.Save(@pictureName, System.Drawing.Imaging.ImageFormat.Png);
}
else if (extension == ".bmp")
{
bmp.Save(@pictureName, System.Drawing.Imaging.ImageFormat.Bmp);
}
else
{
bmp.Save(@pictureName, System.Drawing.Imaging.ImageFormat.Tiff);
}
bmp.Dispose();
MessageBox.Show("导出成功!");
}
}