public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
var files = context.Request.Files[0];
int x = (int)Convert.ToDouble(context.Request["name"].Split(',')[0].Split(':')[1]);
int y = (int)Convert.ToDouble(context.Request["name"].Split(',')[1].Split(':')[1]);
int w = (int)Convert.ToDouble(context.Request["name"].Split(',')[2].Split(':')[1]);
int h = (int)Convert.ToDouble(context.Request["name"].Split(',')[3].Split(':')[1]);
Stream sm = files.InputStream;
byte[] bt = new byte[sm.Length];
sm.Read(bt, 0, files.ContentLength);
System.Drawing.Image bm = System.Drawing.Image.FromStream(CropImageFile(bt, x, y, w, h), true);
bm.Save(context.Request.PhysicalApplicationPath + "upload\\" + files.FileName);
bm.Dispose();
context.Response.Write("Hello World");
}
//裁剪
protected MemoryStream CropImageFile(byte[] imageFile, int targetX, int targetY, int targetW, int targetH)
{
MemoryStream imgMemoryStream = new MemoryStream();
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
Bitmap bmPhoto = new Bitmap(targetW, targetH, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
try
{
grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), targetX, targetY, targetW, targetH, GraphicsUnit.Pixel);
bmPhoto.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception e)
{
throw e;
}
finally
{
imgPhoto.Dispose();
bmPhoto.Dispose();
grPhoto.Dispose();
}
return imgMemoryStream;
}
C# 数据流 截图
最新推荐文章于 2021-03-05 17:21:27 发布