C#实现在服务器端裁剪图片的方法
本文实例讲述了C#实现在服务器端裁剪图片的方法。分享给大家供大家参考。具体实现方法如下:
//图片路径 StringoldPath=Server.MapPath("~/62223231.jpg"); //新图片路径 StringnewPath=System.IO.Path.GetExtension(oldPath); //设置截取的坐标和大小 intx=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.RectanglecropArea=newSystem.Drawing.Rectangle(x,y,width,height);//要截取的区域大小 //加载图片 System.Drawing.Imageimg=System.Drawing.Image.FromStream(newSystem.IO.MemoryStream(System.IO.File.ReadAllBytes(oldPath))); //判断超出的位置否 if((img.Width<x+width)||img.Height<y+height) { Response.Write("截取的区域超过了图片本身的高度、宽度."); img.Dispose(); return; } //定义Bitmap对象 System.Drawing.BitmapbmpImage=newSystem.Drawing.Bitmap(img); //进行裁剪 System.Drawing.BitmapbmpCrop=bmpImage.Clone(cropArea,bmpImage.PixelFormat); //保存成新文件 bmpCrop.Save(newPath); //释放对象 img.Dispose(); bmpCrop.Dispose();
希望本文所述对大家的C#程序设计有所帮助。