[C#] C#在服务器端裁剪图片代码 →→→→→进入此内容的聊天室

来自 , 2020-09-29, 写在 C#, 查看 104 次.
URL http://www.code666.cn/view/18fe8ebf
  1. //图片路径
  2. String oldPath = Server.MapPath("~/62223231.jpg");
  3.  
  4. //新图片路径
  5. String newPath = System.IO.Path.GetExtension(oldPath);
  6.  
  7. //设置截取的坐标和大小
  8. int x = 0, y = 20, width = 200, height = 2400;
  9.  
  10. //计算新的文件名,在旧文件名后加_new
  11. newPath = oldPath.Substring(0, oldPath.Length - newPath.Length) + "_new" + newPath;
  12. Response.Write(oldPath);
  13. Response.Write("<br>");
  14. Response.Write(newPath);
  15. //定义截取矩形
  16. System.Drawing.Rectangle cropArea = new System.Drawing.Rectangle(x, y, width, height); //要截取的区域大小
  17.  
  18. //加载图片
  19. System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(System.IO.File.ReadAllBytes(oldPath)));
  20.  
  21. //判断超出的位置否
  22. if ((img.Width < x + width) || img.Height < y + height)
  23. {
  24.   Response.Write("截取的区域超过了图片本身的高度、宽度.");
  25.   img.Dispose();
  26.   return;
  27. }
  28. //定义Bitmap对象
  29. System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(img);
  30.  
  31. //进行裁剪
  32. System.Drawing.Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
  33.  
  34. //保存成新文件
  35. bmpCrop.Save(newPath);
  36.  
  37. //释放对象
  38. img.Dispose();
  39. bmpCrop.Dispose();
  40. //csharp/6786

回复 "C#在服务器端裁剪图片代码"

这儿你可以回复上面这条便签

captcha