日期:2014-05-17 浏览次数:20381 次
/// <summary> /// 重新生成特定大小的图像 /// </summary> /// <param name="fileName">原图路径</param> /// <param name="path">保存路径</param> /// <param name="height">要生成的高</param> /// <param name="width">要生成的宽</param> void bmpSave(string fileName, string path, int height, int width) { //装载图片 System.Drawing.Image image = System.Drawing.Image.FromFile(fileName); //获取图片的实际宽度与高度 int srcWidth = image.Width; int srcHeight = image.Height; if (height != 0 || width != 0) { //创建Bitmap对象,并设置Bitmap的宽度和高度。 Bitmap bmp = new Bitmap(width, height); //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。 System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp); //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //下面这个也设成高质量 gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //下面这个设成High gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //把原始图像绘制成上面所设置宽高的缩小图 System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, width, height); //暂不理解 gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel); bmp.Save(path, System.Drawing.Imaging.ImageFormat.Bmp); image.Dispose(); } }
------解决方案--------------------
1、自动缩放
2、限定上传尺寸
3、显示时限制显示大小