C# 生成海报,文本区域指定和换行,图片合成

2021-06-16 12:04

阅读:522

标签:添加   rom   指定   pos   font   name   tst   sys   erb   

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                string path = Server.MapPath(@"/Content/images/bg/index_01.jpg");
                AddToImg(path, "图片测试pictureBox在图片上绘制文本_百度知道pictureBox在图片上绘制文本_百度知道", Guid.NewGuid().ToString("N"));
            }
        }

using System.IO;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

绘制图片和文字

        /// 
        /// 指定图片添加指定文字
        /// 
        /// 指定文件路径
        /// 添加的文字
        /// 生成文件名

        private void AddToImg(string file, string text, string picname)
        {
            //判断指定图片是否存在
            //if (!File.Exists(MapPath(fileName)))
            //{
            //    throw new FileNotFoundException("The file don‘t exist!");
            //}

            if (text == string.Empty)
            {
                return;
            }


            System.Drawing.Image image = System.Drawing.Image.FromFile(file);
            Bitmap bitmap = new Bitmap(image, image.Width, image.Height);
            Graphics g = Graphics.FromImage(bitmap);

            float fontSize = 40.0f;             //字体大小
            float textWidth = text.Length * fontSize;  //文本的长度
            //下面定义一个矩形区域,以后在这个矩形里画上白底黑字
            float rectX = 120;
            float rectY = 200;
            float rectWidth = 200;  // text.Length * (fontSize + 40);
            float rectHeight = fontSize + 40;
            //声明矩形域
            RectangleF textArea = new RectangleF(rectX, rectY, 270, 270);


           
            Font font = new Font("华文隶书", fontSize, FontStyle.Bold);   //定义字体
            Font font2 = new Font("楷体", fontSize, FontStyle.Bold);   //定义字体
            //font.Bold = true;

            Brush whiteBrush = new SolidBrush(Color.DodgerBlue);   //白笔刷,画文字用
            //Brush blackBrush = new SolidBrush(Color.Black);   //黑笔刷,画背景用

            //g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);

            g.DrawString(text, font, whiteBrush, textArea, StringFormat.GenericDefault);

            g.DrawString(text, font, whiteBrush, new RectangleF(rectX, image.Height/2, 270, 270));

            g.DrawString("168元", font2, new SolidBrush(Color.Firebrick), new RectangleF(rectX, image.Height - 150, rectWidth, rectHeight));

            ////-------------------  绘制高质量 -------------------------------------------
            //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality 
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //下面这个也设成高质量 
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //下面这个设成High 
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //画专属推广二维码
            System.Drawing.Image qrCodeImage = System.Drawing.Image.FromFile(Server.MapPath(@"/Content/images/money-cards.png"));
            g.DrawImage(qrCodeImage, new Rectangle(image.Width - qrCodeImage.Width - 200,
            image.Height - qrCodeImage.Height - 200,
            qrCodeImage.Width,
            qrCodeImage.Height),
            0, 0, qrCodeImage.Width, qrCodeImage.Height, GraphicsUnit.Pixel);
            //画微信昵称
            g.DrawString("小马快跑", font, new SolidBrush(Color.Red), new Rectangle(image.Width - qrCodeImage.Width - 200,
            image.Height - qrCodeImage.Height - 300,
            qrCodeImage.Width + 100,
            50));

            MemoryStream ms = new MemoryStream();

            //输出方法一:将文件生成并保存到C盘
            string path = "D:\\test\\" + picname + ".png";
            bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);


            //输出方法二,显示在网页中,保存为Jpg类型
            //bitmap.Save(ms, ImageFormat.Jpeg);
            //Response.Clear();
            //Response.ContentType = "image/jpeg";
            //Response.BinaryWrite(ms.ToArray());

            g.Dispose();
            bitmap.Dispose();
            image.Dispose();
        }

 

C# 生成海报,文本区域指定和换行,图片合成

标签:添加   rom   指定   pos   font   name   tst   sys   erb   

原文地址:https://www.cnblogs.com/Fooo/p/10348858.html


评论


亲,登录后才可以留言!