/// <summary>
/// 按比例缩小图片,自动计算高度
/// </summary>
/// <param name="stroldpic">源图文件名(包括路径)</param>
/// <param name="strnewpic">缩小后保存为文件名(包括路径)</param>
/// <param name="intwidth">缩小至宽度</param>
public string smallpic(string stroldpic, string strnewpic, int intwidth)
{
System.Drawing.Bitmap objpic, objnewpic;
try
{
objpic = new System.Drawing.Bitmap(stroldpic);
string path = "";
if (objpic.Width > 500)
{
double intheight = (Convert.ToDouble(intwidth) / Convert.ToDouble(objpic.Width)) * Convert.ToDouble(objpic.Height);
objnewpic = new System.Drawing.Bitmap(objpic, intwidth, Convert.ToInt32(intheight));
System.Drawing.Imaging.EncoderParameter p;
System.Drawing.Imaging.EncoderParameters ps;
ps = new System.Drawing.Imaging.EncoderParameters(1);
p = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80L);
ps.Param[0] = p;
objnewpic.Save(strnewpic,GetCodecInfo("image/jpeg"), ps);
path = "1";
}
return path;
}
catch (Exception exp) { throw exp; }
finally
{
objpic = null;
objnewpic = null;
}
}
private static System.Drawing.Imaging.ImageCodecInfo GetCodecInfo(string mimeType)
{
System.Drawing.Imaging.ImageCodecInfo[] CodecInfo = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
foreach (System.Drawing.Imaging.ImageCodecInfo ici in CodecInfo)
{
if (ici.MimeType == mimeType) return ici;
}
return null;
}