C# 数据流 截图

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值