• 头像剪切后图片上传


    前言

    在此只介绍后台,用ashx 举例:

    前端我们要做的事情,就是将图片从哪个位置截取,也就是位置x和y。

    然后截取的宽和高,width 和 height。

    现在我们知道的就是需要四个数据x、y、width、height。

    代码

    int x = Convert.ToInt32(context.Request["x"]);
    int y = Convert.ToInt32(context.Request["y"]);
    int height = Convert.ToInt32(context.Request["height"]);
    int width = Convert.ToInt32(context.Request["width"]);
    string imageUrl = context.Request["imageUrl"];
    using (Bitmap map=new Bitmap(width,height))
    {
    	using (Graphics g = Graphics.FromImage(map))
    	{                   
    		using (Image img = Image.FromFile(context.Request.MapPath(imageUrl)))
    		{
    			g.DrawImage(img,new Rectangle(0,0,width,height),new Rectangle(x,y,width,height),GraphicsUnit.Pixel);
    			string fileNewName = Guid.NewGuid().ToString();
    			string dir = "/ashx/UPLoad/Image/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
    			string fulldir = dir + fileNewName + ".jpg";
    			map.Save(context.Request.MapPath(fulldir),System.Drawing.Imaging.ImageFormat.Jpeg);
    			//处理数据库回调。。。
    		} 
    	}
    }
    

    当年写的代码,见谅。

    分析一下,当我们得到了x、y、height、width。

    然后我们创建了bitmap,然后创建了graphics 画布,然后开始切割了。

    g.DrawImage 就开始直接在画布上画图了。

    解释一下参数的意思:

    第一个参数image是图片源。
    
    第二个参数:
    
    destRect
        Rectangle
    Rectangle 结构,它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。
    
    第三个参数:
    
    srcRect
        Rectangle
    
    Rectangle 结构,它指定 image 对象中要绘制的部分。
    
    第四个是以什么来计算,一般来说是以像素来计算的。
    

    然后呢,如果看完是可以发现上面的代码发现有问题的。

    问题在于切割的后其实我们应该保持相同大小的,而我们的头像一般要是正方形。

    当然只是业务问题,只是介绍大概思路。

  • 相关阅读:
    期末考试冬眠
    题解 P1457 【城堡 The Castle】
    题解 P1052 【过河】
    题解 P5663 【加工零件【民间数据】】
    与 macOS 10.12 兼容的OpenCV版本
    summarise() regrouping output 警告
    binary_crossentropy和BinaryCrossentropy的区别
    损失函数BinaryCrossentropy例子说明
    理解功能强大的sed替换命令
    理解GAN对抗神经网络的损失函数和训练过程
  • 原文地址:https://www.cnblogs.com/aoximin/p/13063147.html
Copyright © 2020-2023  润新知