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


     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;

    绘制图片和文字

            /// <summary>
            /// 指定图片添加指定文字
            /// </summary>
            /// <param name="fileName">指定文件路径</param>
            /// <param name="text">添加的文字</param>
            /// <param name="picname">生成文件名</param>
    
            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();
            }
  • 相关阅读:
    领域驱动模型DDD(二)——领域事件的订阅/发布实践
    领域驱动模型DDD(一)——服务拆分策略
    js创建对象的多种方式
    java.net.SocketException: Connection reset 异常处理
    hive从入门到放弃(四)——分区与分桶
    《前端运维》五、k8s3灰度发布、滚动更新与探针
    《前端运维》五、k8s2pod、services与Ingress部署
    《前端运维》三、Docker2其他
    《前端运维》三、Docker1镜像与容器
    《前端运维》二、Nginx3静态资源服务、跨域与其他
  • 原文地址:https://www.cnblogs.com/Fooo/p/10348858.html
Copyright © 2020-2023  润新知