C#在服务器端裁剪图片代码
发布时间:2020-12-15 17:55:49 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 //图片路径String oldPath = Server.MapPath("~/62223231.jpg"); //新图片路径String newPath = System.IO.Path.GetExtension(oldPath); //设置截取的
|
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 //图片路径
String oldPath = Server.MapPath("~/62223231.jpg");
//新图片路径
String newPath = System.IO.Path.GetExtension(oldPath);
//设置截取的坐标和大小
int x = 0,y = 20,width = 200,height = 2400;
//计算新的文件名,在旧文件名后加_new
newPath = oldPath.Substring(0,oldPath.Length - newPath.Length) + "_new" + newPath;
Response.Write(oldPath);
Response.Write("<br>");
Response.Write(newPath);
//定义截取矩形
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();
以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
