在VS2008(.net 2.0以上版本)下使用如下代码保存图片:
private void btnSave_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "PNG Image|*.png|JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
saveFileDialog1.Title = "Save";
saveFileDialog1.FileName = string.Empty;
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();
switch (saveFileDialog1.FilterIndex)
{
case 1:
this.picEncode.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2:
((Bitmap)this.picEncode.Image).Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 3:
((Bitmap)this.picEncode.Image).Save(fs,
System.Drawing.Imaging.ImageFormat.Gif);
break;
case 4:
((Bitmap)this.picEncode.Image).Save(fs,
System.Drawing.Imaging.ImageFormat.Png);
break;
}
fs.Close();
fs.Dispose();
}
}
注意上面case 1和4略有区别。结果都一样:保存的图片在ACD SEE, Windows图片查看器或自带画图板中均可以打开,但使用Photoshop打开图片时,则报错:
(PNG格式)
(JPG格式)
还好,几年前写的一个保存图片的类被翻了出来:
// SavePhoto.cs
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace BrawDraw.Com.PhotoFrame.Net.PublicFunctions
{
/// <summary>
/// SavePhotos 的摘要说明。
/// </summary>
public class SavePhoto
{
private Bitmap _bitmapForSave;
private string _targetFileName;
private string _targetFolder;
public SavePhoto()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public Bitmap BitmapForSave
{
get
{
return _bitmapForSave;
}
set
{
_bitmapForSave = value;
}
}
public string TargetFileName
{
get
{
return _targetFileName;
}
set
{
_targetFileName = value;
}
}
public string TargetFolder
{
get
{
if( _targetFolder == string.Empty || _targetFolder == null )
{
_targetFolder = @"c:\";
}
return _targetFolder;
}
set
{
_targetFolder = value;
}
}
public string SaveWithDialog()
{
return SaveWithDialog( TargetFolder,string.Empty);
}
public string SaveWithDialog(string initDir,string fmt)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if(initDir==null || initDir==string.Empty)
{
initDir = @"C:\" ;
}
saveFileDialog.InitialDirectory = initDir;
string strFilters;
string strFiltersBmp="Bitmap files (*.bmp)|*.bmp";
string strFiltersGif="Gif files (*.gif)|*.gif";
string strFiltersJpg="Jpeg files (*.jpg,jpeg,jpe)|*.jpg;*.jpeg;*.jpe";
string strFiltersPng="PNG files (*.png)|*.png";
string strFiltersTif="Tiff files (*.tif,*tiff)|*.tif;*.tiff";
string strFiltersAll="All files (*.*)|*.*";
switch(fmt.ToLower())
{
case "bmp":
strFilters = strFiltersBmp;
break;
case ".gif":
strFilters = strFiltersGif;
break;
case ".jpg":
case ".jpeg":
case ".jpe":
strFilters = strFiltersJpg;
break;
case ".png":
strFilters = strFiltersPng;
break;
case ".tif":
case ".tiff":
strFilters = strFiltersTif;
break;
default:
strFilters = strFiltersJpg +"|"+ strFiltersBmp +"|" + strFiltersGif +"|"+ strFiltersPng +"|"+ strFiltersTif +"|"+ strFiltersAll;
break;
}
saveFileDialog.Filter = strFilters;
saveFileDialog.FilterIndex = 1 ;
saveFileDialog.RestoreDirectory = true ;
saveFileDialog.AddExtension=true;
string strFileName = string.Empty;
if(DialogResult.OK == saveFileDialog.ShowDialog())
{
strFileName = saveFileDialog.FileName;
bool resultSave = Save(strFileName);
if(!resultSave)
{
strFileName = string.Empty;
}
}
return strFileName;
}
public bool Save(Bitmap bitmapOfSave)
{
if( _bitmapForSave == null)
{
_bitmapForSave = bitmapOfSave;
}
try
{
SaveWithDialog( TargetFolder, "All");
}
catch(Exception ex)
{
MessageBox.Show("出错了!" + ex.Message);
return false;
}
return true;
// finally
// {
// if( m_Bitmap != null)
// m_Bitmap.Dispose();
// }
}
public bool Save(string strFileName)
{
return Save(strFileName,string.Empty);
}
public bool Save(string strFileName,string addString)
{
string[] arrStrFileName = strFileName.Replace(@"\", @"/").Split(new char[]{'/'});
if(arrStrFileName.Length <= 1)
{
strFileName = TargetFolder + @"\" + strFileName;
}
ImageCodecInfo myImageCodecInfo;
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
string strFileExt;
if(strFileName.LastIndexOf(@".")<=0)
{
strFileExt=".jpg";
strFileName += (addString + ".jpg");
}
else
{
strFileExt = strFileName.Substring(strFileName.LastIndexOf(@"."));
string strCurrentPhotoPath = strFileName.Substring(0,strFileName.LastIndexOf(@"\")+1);
strFileName = strCurrentPhotoPath + Path.GetFileNameWithoutExtension(strFileName) + addString + strFileExt;
}
try
{
switch (strFileExt.ToLower())
{
case ".bmp":
_bitmapForSave.Save(strFileName, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case ".gif":
_bitmapForSave.Save(strFileName, System.Drawing.Imaging.ImageFormat.Gif);
break;
case ".jpg":
case ".jpeg":
case ".jpe":
//获取JPEG格式的编码方式
myImageCodecInfo = GetEncoderInfo("image/jpeg");
//分别设置JPEG文件的图片质量
//编码参数种类为Quality
myEncoder = System.Drawing.Imaging.Encoder.Quality;
//创建一个EncoderParameters对象,它仅包含一个 EncoderParameter对象
myEncoderParameters = new EncoderParameters(1);
//设置JPEG图片质量为80级
myEncoderParameter = new EncoderParameter(myEncoder,80L);
myEncoderParameters.Param[0] = myEncoderParameter;
_bitmapForSave.Save(strFileName, myImageCodecInfo, myEncoderParameters);
break;
case ".png":
_bitmapForSave.Save(strFileName, System.Drawing.Imaging.ImageFormat.Png);
break;
case ".tif":
case ".tiff":
//获取JPEG格式的编码方式
myImageCodecInfo = GetEncoderInfo("image/tiff");
//创建一个描述压缩方式的参数对象
myEncoder= System.Drawing.Imaging.Encoder.Compression;
myEncoderParameters = new EncoderParameters(1);
//设置JPEG图片质量为25级
myEncoderParameter = new EncoderParameter(
myEncoder, (long)EncoderValue.CompressionLZW
);
myEncoderParameters.Param[0] = myEncoderParameter;
_bitmapForSave.Save(strFileName, myImageCodecInfo, myEncoderParameters);
break;
default:
_bitmapForSave.Save(strFileName + @".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
}
catch(Exception exc)
{
MessageBox.Show("Error:" + exc.Message);
return false;
}
return true;
}
public static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
}
public enum SavePhotoResult
{
Yes,
No,
Cancel
}
}
// 具体调用时的示例代码:
using BrawDraw.Com.PhotoFrame.Net.PublicFunctions;
private void btnSave_Click(object sender, EventArgs e)
{
SavePhoto savePhoto = new SavePhoto();
savePhoto.BitmapForSave = (Bitmap)this.picEncode.Image;
savePhoto.TargetFolder = @"c:\";
savePhoto.SaveWithDialog();
}