Net Core教程

C#分享海报生成

本文主要是介绍C#分享海报生成,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
using Microsoft.AspNetCore.Mvc;
using Net5ApiDemo.Common;
using System;
using System.Drawing;
using System.IO;

namespace Net5ApiDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        public TestController()
        {

        }
        [HttpGet]
        public IActionResult Get()
        {
            var res = makePic();
            return Content(res);
        }


        /// <summary>
        /// 生成海报
        /// </summary>
        public string makePic()
        {
            int imgWidth = 750, imgHeight = 1363;

            var imgUrl = "";
            string imgHeadPath = @"E:/ShareImg/sharebg.jpg";//要插入的抬头图片路径 可以从数据库取
            string imgQrPath = @"E:/ShareImg/qrcode.png";  //要插入的二维码图片路径 绝对路径
            string imgAddPath = @"E:/ShareImg/add.png";  //要插入的加入我们图片路径 绝对路径
            string imgWxHeadPath2 = @"https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eqYXbkXu0CrloZRIfVLLEsezx78PXicB3210fgSW3Os8IIQ8st0ztqYLJ9Ux2wR8icicDxw8Uvklj2LQ/132";

            string savePath = Environment.CurrentDirectory + @"/TempImg/"; //文件路径
            var imgWxHeadPath = Util.HttpDownloadFile(imgWxHeadPath2, savePath);

            Bitmap bitmapPic = new Bitmap(imgWidth, imgHeight);// 新建一个 400*600的Bitmap 位图 
            Graphics g = Graphics.FromImage(bitmapPic);// 根据新建的 Bitmap 位图,创建画布
            g.Clear(Color.White);// 使用白色重置画布

            Image headPic = Image.FromFile(imgHeadPath);//抬头
            Image ewmPic = Image.FromFile(imgQrPath);//二维码
            Image addPic = Image.FromFile(imgAddPath);//加入我们

            Image wxHeadPic = Image.FromFile(imgWxHeadPath);//微信头像
            Image wxHeadPic2 = CutEllipse(wxHeadPic, new Rectangle(0, 0, 132, 132), new Size(60, 60));//生成圆形头像


            //在背景图片上绘制指定的二维码图片 和抬头图片
            g.DrawImage(headPic, 0, 0, imgWidth, imgHeight);
            g.DrawImage(ewmPic, imgWidth / 3 * 2, imgHeight / 8 * 7 + 20, 124, 124);
            g.DrawImage(addPic, imgWidth / 2 - 258-20, imgHeight / 8 * 7 + 20, 258, 45);

            g.DrawImage(wxHeadPic2, imgWidth / 2 - 258-20, imgHeight / 8 * 7 + 80, 60, 60);//绘制60*60的圆形头像


            //在背景照片上添加文字  
            PointF drawPoint = new PointF(imgWidth / 4F-10, 1270.0F);
            AddFont(g, drawPoint, "姓名:张三");
            drawPoint = new PointF(imgWidth / 4F-10, 1305.0F);
            AddFont(g, drawPoint, "电话:13122222222");
            
            //AddFont(g, drawPoint, "....................");

            //g.FillRectangle(Brushes.Pink, 0, 570, 400, 30);//底部的矩形填充 填充由一对坐标,一个宽度和一个高度指定的矩形的内部
            //drawPoint = new PointF(0F, 570F);
            //AddFont(g, drawPoint, "识别二维码查看更多优质文章^_^");

            using (MemoryStream ms = new MemoryStream())
            {
                //将背景图存到内存流(ms) 参数2;文件格式
                bitmapPic.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//将此图像以指定的格式保存到指定的流中
                                                                           //在本页面显示图片
                imgUrl = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
            }
            return imgUrl;
        }
        /// <summary>
        /// 在图片上添加文字
        /// </summary>
        /// <param name="g"><目标Graphics对象/param>
        /// <param name="drawPoint"></param>
        /// <param name="data">准备添加的字符串</param>
        private void AddFont(Graphics grap, PointF drawPoint, string data)
        {
            SolidBrush mybrush = new SolidBrush(Color.GhostWhite);    //设置默认画笔颜色
            Font myfont = new Font("黑体", 19);   //设置默认字体格式   
            grap.DrawString(data, myfont, mybrush, drawPoint);  //图片上添加文字;
        }
        private Image CutEllipse(Image img, Rectangle rec, Size size)
        {
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
                {
                    br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.FillEllipse(br, new Rectangle(Point.Empty, size));
                }
            }
            return bitmap;
        }
    }
}
View Code

效果图:

 

这篇关于C#分享海报生成的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!