/// <summary>
/// 修改图片分辨率并保存图片
/// </summary>
/// <param name="oldPath">原图片路径</param>
/// <param name="fileName">图片名</param>
/// <param name="path">图片存储路径</param>
/// <param name="width">图片宽度</param>
/// <param name="height">图片高度</param>
/// <returns></returns>
public int CreatePic(string oldPath, string fileName, string path, int width, int height)
{
try
{
if (width != 0 && height != 0)
{
//System.Drawing.Bitmap ret = null;
System.Drawing.Image source = Image.FromFile(oldPath);
CreateCompressThumbnail(source, width, height, path, fileName);
}
else
{
FileStream fs = File.OpenRead(oldPath);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
FileStream file = new FileStream(path + fileName, FileMode.OpenOrCreate);
file.Write(bytes, 0, bytes.Length);
file.Close();
}
return 1;
}
catch
{
return 0;
}
}
private static Bitmap CreateThumbnail(Image source, int thumbWi, int thumbHi)
{
System.Drawing.Bitmap ret = null;
try
{
int wi, hi;
wi = thumbWi;
hi = thumbHi;
ret = new Bitmap(wi, hi);
using (Graphics g = Graphics.FromImage(ret))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, wi, hi);
g.DrawImage(source, 0, 0, wi, hi);
g.Dispose();
}
}
catch
{
ret = null;
}
return ret;
}
public static void CreateCompressThumbnail(Image source, int thumbWi, int thumbHi, string SavePathThumb, string fileName)
{
using (System.Drawing.Image myThumbnail = CreateThumbnail(source, thumbWi, thumbHi))
{
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long[] quality = new long[1];
quality[0] = 75;
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];
break;
}
}
myThumbnail.Save(Path.Combine(SavePathThumb, fileName), jpegICI, encoderParams);
myThumbnail.Dispose();
}
}