首页 > 代码库 > asp.net 图片裁切
asp.net 图片裁切
//图片路径
String oldPath = Server.MapPath("/111.jpg");
//新图片路径
String newPath = System.IO.Path.GetExtension(oldPath);
//设置截取的坐标和大小
int x = 500, y = 0, width = 500, height = 500;
//计算新的文件名,在旧文件名后加_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();
Response.Write("裁切成功");
String oldPath = Server.MapPath("/111.jpg");
//新图片路径
String newPath = System.IO.Path.GetExtension(oldPath);
//设置截取的坐标和大小
int x = 500, y = 0, width = 500, height = 500;
//计算新的文件名,在旧文件名后加_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();
Response.Write("裁切成功");
asp.net 图片裁切
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。