C#在服务器端裁剪图片源码

下边内容段是关于C#在服务器端裁剪图片的内容,应该能对小伙伴们有较大用途。

String oldPath = Server.MapPath("~/62223231.jpg");

String newPath = System.IO.Path.GetExtension(oldPath);

int x = 0, y = 20, width = 200, height = 2400;

newPath = oldPath.Substring(0, oldPath.Length - newPath.Length) + “_new” + newPath;
Response.Write(oldPath);
Response.Write("
");
Response.Write(newPath);

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;
}
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();

你可能感兴趣的:(C#)