• C# Graphic 操作记录


    C# 在图片上绘制文字

      		/// <summary>
            /// 生成文字图片
            /// </summary>
            /// <param name="text"></param>
            /// <param name="isBold"></param>
            /// <param name="fontSize"></param>
            public static Image CreateImage(string text, bool isBold, int fontSize)
            {
                int wid = 400;
                int high = 200;
                Font font;
                if (isBold)
                {
                    font = new Font("Arial", fontSize, FontStyle.Bold);
                }
                else
                {
                    font = new Font("Arial", fontSize, FontStyle.Regular);
                }
                //绘笔颜色
                SolidBrush brush = new SolidBrush(Color.Black);
                StringFormat format = new StringFormat(StringFormatFlags.NoClip);
                Bitmap image = new Bitmap(wid, high);
                Graphics g = Graphics.FromImage(image);
                SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);//得到文本的宽高
                int width = (int)(sizef.Width + 1);
                int height = (int)(sizef.Height + 1);
                image.Dispose();
                image = new Bitmap(width, height);
                g = Graphics.FromImage(image);
                g.Clear(Color.White);//透明
    
                RectangleF rect = new RectangleF(0, 0, width, height);
                //绘制图片
                g.DrawString(text, font, brush, rect);
                //释放对象
                g.Dispose();
                return image;
            }
    

    合并两张图片

            /// <summary>
            /// 合并图片
            /// </summary>
            /// <param name="imgBack"></param>
            /// <param name="img"></param>
            /// <returns></returns>
            public static Bitmap CombinImage(Image imgBack, Image img, int xDeviation = 0, int yDeviation = 0)
            {
                Bitmap bmp = new Bitmap(imgBack.Width, imgBack.Height + img.Height);
    
                Graphics g = Graphics.FromImage(bmp);
                g.Clear(Color.White);
                g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
    
                //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框
    
                //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
    
                g.DrawImage(img, imgBack.Width / 2 - img.Width / 2 + xDeviation, imgBack.Height + yDeviation, img.Width, img.Height);
                GC.Collect();
                return bmp;
            }
    

    原文学习链接: https://www.cnblogs.com/stulzq/p/6137715.html

  • 相关阅读:
    白话https
    网络分层体系结构
    软件度量
    【Java】itext根据模板生成pdf(包括图片和表格)
    【java】Freemarker 动态生成word(带图片表格)
    压力测试-接口关联
    压力测试-录制脚本
    jmeter性能测试2:基础功能介绍
    jmeter性能测试1:目录解析
    桃花运
  • 原文地址:https://www.cnblogs.com/runningRain/p/13085961.html
Copyright © 2020-2023  润新知