C# 对图片进行缩放

来源:互联网 发布:淘宝网的评价管理在哪 编辑:程序博客网 时间:2024/04/27 16:30

 为了提高与用户的交互力,很多网站都有图片上传之类的功能,

图片过大在显示时是很占用资源的(为了布局用IMG控制成小图更亏),

下面便用两个简单的方法,为大图片生成需要的小图片

 

方法1: 使用 System.Drawing.Image

  1.     /// <summary>
  2.     /// 对图片进行处理,返回一个Image类别的对象
  3.     /// </summary>
  4.     /// <param name="oldImgPath">原图片路径</param>
  5.     /// <param name="newWidth">新图片宽度</param>
  6.     /// <param name="newHeight">新图片高度</param>
  7.     /// <returns></returns>
  8.     public static Image GetNewImage(string oldImgPath, int newWidth, int newHeight)
  9.     {
  10.         Image oldImg = Image.FromFile(oldImgPath); // 加载原图片
  11.         Image newImg = oldImg.GetThumbnailImage(newWidth, newHeight, new Image.GetThumbnailImageAbort(IsTrue), IntPtr.Zero); // 对原图片进行缩放
  12.         return newImg;
  13.     }
  14.     private static bool IsTrue() // 在Image类别对图片进行缩放的时候,需要一个返回bool类别的委托
  15.     {
  16.         return true;
  17.     }

方法2: 使用 System.Drawing.Bitmap

  1.     /// <summary>
  2.     /// 对图片进行处理,返回一个Bitmap类别的对象
  3.     /// </summary>
  4.     /// <param name="oldBmpPath">原图片路径</param>
  5.     /// <param name="newWidth">新图片宽度</param>
  6.     /// <param name="newHeight">新图片高度</param>
  7.     /// <returns></returns>
  8.     public static Bitmap GetNewBitMap(string oldBmpPath, int newWidth, int newHeight)
  9.     {
  10.         Bitmap oldBmp = new Bitmap(oldBmpPath); // 加载原图片
  11.         Bitmap bmp = new Bitmap(newWidth, newHeight); // 创建新图片
  12.         Graphics grap = Graphics.FromImage(bmp); // 绑定画板
  13.         // 原图片的开始绘制位置,及宽和高 (控制Rectangle的组成参数,便可实现对图片的剪切)
  14.         Rectangle oldRect = new Rectangle(0, 0, oldBmp.Width, oldBmp.Height);
  15.         // 绘制在新画板中的位置,及宽和高 (在这里是完全填充)
  16.         Rectangle newRect = new Rectangle(0, 0, newWidth, newHeight);
  17.         // 指定新图片的画面质量
  18.         grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
  19.         // 把原图片指定位置的图像绘制到新画板中
  20.         grap.DrawImage(oldBmp, newRect, oldRect, GraphicsUnit.Pixel);
  21.         /*
  22.          * 画图的步骤到此就已经完成了.
  23.          * 
  24.          * 在绘制完成新图片后,还可以使用 Graphics对象的一些方法,为图片添加自定义的内容
  25.          * grap.DrawString(...);添加文字
  26.          * grap.DrawPie(...);添加扇形
  27.          * grap.DrawLine(...);添加直线
  28.          * ...
  29.          * */
  30.         // 添加文字
  31.         Brush bru = Brushes.Red; // 笔刷
  32.         Font font = new Font(new FontFamily("华文行楷"), 30, FontStyle.Regular, GraphicsUnit.World); // 字体
  33.         PointF pf = new PointF(3, 3); // 坐标
  34.         grap.DrawString("羊", font, bru, pf); // 填充文字
  35.         return bmp;
  36.     }

调用方法:

  1.     protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.         Image img = UpdateImage.GetNewImage(FileUpload1.PostedFile.FileName/*图片路径*/, 200, 200);
  4.         img.Save(Server.MapPath("img_XXX.jpg"));
  5.         Bitmap bmp = UpdateImage.GetNewBitMap(FileUpload1.PostedFile.FileName/*图片路径*/, 200, 200);
  6.         bmp.Save(Server.MapPath("bm_XXX.jpg"));
  7.     }
原创粉丝点击