你的位置:首页 > ASP.net教程

[ASP.net教程]c#.net 生成清晰缩略图


 1 public void imgsize() 2     { 3       //本例中假定了两个变量: 4  5       String src = "c:/myImages/a.jpg";  //源图像文件的绝对路径 6       String dest = "c:/myImages/a_th.jpg";  //生成的缩略图图像文件的绝对路径 7  8       int thumbWidth = 132;  //要生成的缩略图的宽度 9       int thumbHeight = 100;  //要生成的缩略图的高度10       System.Drawing.Image image = System.Drawing.Image.FromFile(src); //利用Image对象装载源图像11 12       //接着创建一个System.Drawing.Bitmap对象,并设置你希望的缩略图的宽度和高度。13       int srcWidth = image.Width;14       int srcHeight = image.Height;15       Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);16 17       //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。18       System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);19 20       //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality21       gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;22 23       //下面这个也设成高质量24       gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;25 26       //下面这个设成High27       gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;28 29       //把原始图像绘制成上面所设置宽高的缩小图30       System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);31       gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);32 33       //保存图像,大功告成!34       bmp.Save(dest);35 36       //最后别忘了释放资源37       bmp.Dispose();38       image.Dispose();39     }