日期:2014-05-17 浏览次数:20617 次
/// <summary>
/// 图片缩放
/// </summary>
/// <param name="sourcePath">客户端路径</param>
/// <param name="savePath">保存服务器端路径</param>
/// <param name="w">宽</param>
/// <param name="h">高</param>
public static void ImageChange(string sourcePath, string savePath, int w, int h)
{
System.Drawing.Image _sourceImg = System.Drawing.Image.FromFile(sourcePath);
double _newW = (double)w, _newH = (double)h, t;
if ((double)_sourceImg.Width > w)
{
t = (double)w;
}
else
{
t = (double)_sourceImg.Width;
}
if ((double)_sourceImg.Height * (double)t / (double)_sourceImg.Width > (double)h)
{
_newH = (double)h;
_newW = (double)h / (double)_sourceImg.Height * (double)_sourceImg.Width;
}
else
{
_newW = t;
_newH = (t / (double)_sourceImg.Width) * (double)_sourceImg.Height;
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap((int)_newW, (int)_newH);
System.Drawing.Graphics g = Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(_sourceImg, new Rectangle(0, 0, (int)_newW, (int)_newH), new Rectangle(0, 0, _sourceImg.Width, _sourceImg.Height), GraphicsUnit.Pixel);
_sourceImg.Dispose();
g.Dispose();
try
{
bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch
{
}
bitmap.Dispose();
}