分享

一段裁切图片的代码 Asp.Net Cropper Image

 悟静 2012-06-12

完整代码如下:

C# 代码
//图片路径
String oldPath = Server.MapPath("~/aaaa.jpg");

//新图片路径
String newPath = System.IO.Path.GetExtension(oldPath);

//设置截取的坐标和大小
int x = 0, y = 10, width = 200, height = 200;

//计算新的文件名,在旧文件名后加_new
newPath = oldPath.Substring(0, oldPath.Length - newPath.Length) + "_new" + newPath;
Response.Write(oldPath);
Response.Write(
"<br>");
Response.Write(newPath);
Response.Write(
"<br>");
//定义截取矩形
System.Drawing.Rectangle cropArea = new System.Drawing.Rectangle(x, y, width, height); //要截取的区域大小

//加载图片
System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(System.IO.File.ReadAllBytes(oldPath)));

//判断超出的位置否
if ((img.Width < x + width) || img.Height < y + height)
{
  Response.Write(
"截取的区域超过了图片本身的高度、宽度.");
  img.Dispose();
  
return;
}
//定义Bitmap对象
System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(img);

//进行裁剪
System.Drawing.Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);

//保存成新文件
bmpCrop.Save(newPath);

//释放对象
img.Dispose();
bmpCrop.Dispose();

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约