.Net (二维码)图片海报生成

 public string Draw()
    {
        //背景图片,海报背景
        string path = Server.MapPath("/wwwroot/bg.jpg");
 
        System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(path);
 
        //处理二维码图片大小 240*240px
        System.Drawing.Image qrCodeImage = ReduceImage("https://images.cnblogs.com/blog/10086/202010/10086-20201015151635.png", 0, 0);
 
        //处理头像图片大小 100*100px,需要则取消注释
        //Image titleImage = ReduceImage(user.headimgurl, 100, 100);
 
        using (Graphics g = Graphics.FromImage(imgSrc))
        {
            //画专属推广二维码
            g.DrawImage(qrCodeImage, new Rectangle(imgSrc.Width - qrCodeImage.Width -450,//-450越小越靠左,调整二维码在背景图的位置
            imgSrc.Height - qrCodeImage.Height-650 ,//同理-650越小越靠上
            qrCodeImage.Width,
            qrCodeImage.Height),
            0, 0, qrCodeImage.Width, qrCodeImage.Height, GraphicsUnit.Pixel);
 
            //画头像
            //g.DrawImage(titleImage, 8, 8, titleImage.Width, titleImage.Height);
 
            Font font = new Font("新宋体", 30, FontStyle.Bold);
 
            g.DrawString("海报文字", font, new SolidBrush(Color.Red), 500, 800);
        }
        string newpath = Server.MapPath(@"/wwwroot/hb_" + Guid.NewGuid().ToString() + ".jpg");
        imgSrc.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
        return newpath;
    }
 
//获取图片并处理成指定只存返回,宽高填写0,直接返回原尺寸
public Image ReduceImage(string url, int toWidth, int toHeight)
    {
        //这里网络方式获取图片二维码,本地读取请自己写
        WebRequest request = WebRequest.Create(url);
        WebResponse response = request.GetResponse();
        Stream responseStream = response.GetResponseStream();
 
        Image originalImage = Image.FromStream(responseStream);
        if (toWidth <= 0 && toHeight <= 0)
        {
            return originalImage;//这里直接返回
        }
        else if (toWidth > 0 && toHeight > 0)
        {
            if (originalImage.Width < toWidth && originalImage.Height < toHeight)
                return originalImage;
        }
        else if (toWidth <= 0 && toHeight > 0)
        {
            if (originalImage.Height < toHeight)
                return originalImage;
            toWidth = originalImage.Width * toHeight / originalImage.Height;
        }
        else if (toHeight <= 0 && toWidth > 0)
        {
            if (originalImage.Width < toWidth)
                return originalImage;
            toHeight = originalImage.Height * toWidth / originalImage.Width;
        }
        Image toBitmap = new Bitmap(toWidth, toHeight);
        using (Graphics g = Graphics.FromImage(toBitmap))
        {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);
            g.DrawImage(originalImage,
                        new Rectangle(0, 0, toWidth, toHeight),
                        new Rectangle(0, 0, originalImage.Width, originalImage.Height),
                        GraphicsUnit.Pixel);
            originalImage.Dispose();
            return toBitmap;
        }
    }

 

posted @ 2020-10-15 15:15  ZeroBug  阅读(392)  评论(0编辑  收藏  举报