欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#长图文(横版实现文字换行)相关实现

程序员文章站 2022-05-29 16:25:21
...

C#长图文(横版实现文字换行)相关实现C#长图文(横版实现文字换行)相关实现

效果如上图

代码如下:

/// <summary>
        /// 横版长图文文字从左起
        /// </summary>
        /// <param name="template">底图</param>
        /// <param name="str">文本内容  传入文本段首没有空两格</param>
        /// <param name="font">字体</param>
        /// <param name="color">字色</param>
        /// <param name="top">上边边距</param>
        /// <param name="bottom">下边边距</param>
        /// <param name="left">两边边距</param>
        /// <param name="lineH">列间距</param>
        /// <returns></returns>
        public static Bitmap LongPic_H_left(Bitmap template, string str, Font font, Color color, int top, int bottom, int left, int lineH)
        {
            int h = template.Height;
            int w = CalculateW(str, h, font, top, bottom, left, lineH);
            Bitmap bg = new Bitmap(w, h);
            Graphics g = Graphics.FromImage(bg);
            g.DrawImage(template, 0, 0, w, h);
            Brush c = new SolidBrush(color);
            string[] strs = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            int x = left;
            int y = top;
            int w1 = (int)(Math.Ceiling(font.Size) / 72 * 96) + lineH;//两列字Y的差距
            int row_H = (h - top - bottom);//一列文字可画区域的高度
            int a = font.Height;  //字体高度
            int b = row_H / a;  //一列可容纳的个数
            foreach (string i in strs)
            {
                string s = i.ToString();
                int len = s.Length;
                for (int j1 = 0, j2 = 1; j1 < len; j1++, j2++)
                {
                    if (j2 > b)
                    {
                        x += w1;
                        y = top;
                        j2 = 1;
                    }
                    string s1 = s[j1].ToString();
                    g.DrawString(s1, font, c, x, y);
                    y += font.Height;
                }
                x += w1;
                y = top;
            }
            return bg;
        }
        /// <summary>
        /// 横版长图文文字从右起
        /// </summary>
        /// <param name="template">底图</param>
        /// <param name="str">文本内容  传入文本段首没有空两格</param>
        /// <param name="font">字体</param>
        /// <param name="color">字色</param>
        /// <param name="top">上边边距</param>
        /// <param name="bottom">下边边距</param>
        /// <param name="left">两边边距</param>
        /// <param name="lineH">列间距</param>
        /// <returns></returns>
        public static Bitmap LongPic_H_right(Bitmap template, string str, Font font, Color color, int top, int bottom, int left, int lineH)
        {
            int h = template.Height;
            int w = CalculateW(str, h, font, top, bottom, left, lineH);
            Bitmap bg = new Bitmap(w, h);
            Graphics g = Graphics.FromImage(bg);
            g.DrawImage(template, 0, 0, w, h);
            Brush c = new SolidBrush(color);
            string[] strs = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            int x = w - left;
            int y = top;
            int w1 = (int)(Math.Ceiling(font.Size) / 72 * 96) + lineH;//两列字Y的差距
            int row_H = (h - top - bottom);//一列文字可画区域的高度
            int a = font.Height;  //字体高度
            int b = row_H / a;  //一列可容纳的个数
            foreach (string i in strs)
            {
                string s = i.ToString();
                int len = s.Length;
                for (int j1 = 0, j2 = 1; j1 < len; j1++, j2++)
                {
                    if (j2 > b)
                    {
                        x -= w1;
                        y = top;
                        j2 = 1;
                    }
                    string s1 = s[j1].ToString();
                    g.DrawString(s1, font, c, x, y);
                    y += font.Height;
                }
                x -= w1;
                y = top;
            }
            return bg;
        }
        /// <summary>
        /// 横版长图文计算生成后图片宽度
        /// </summary>
        /// <param name="content">文本内容   传入文本段首没有空两格</param>
        /// <param name="h">底图高度</param>
        /// <param name="font">字体</param>
        /// <param name="top">上边边距</param>
        /// <param name="bottom">底边边距</param>
        /// <param name="left">两边边距</param>
        /// <param name="lineH">列间距</param>
        /// <returns></returns>
        public static int CalculateW(string content, int h, Font font, int top, int bottom, int left, int lineH)
        {
            string[] str = content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            int tmp = 0;//累计行数初始为0
            foreach (string i in str)
            {
                string s = i.ToString();
                tmp += RowNum_H(s, h, font, top, bottom);
            }
            int fh = (int)(Math.Ceiling(font.Size) / 72 * 96);//单字的宽度
            int W = tmp * (fh + lineH) + left * 2;
            return W;
        }
        /// <summary>
        /// 横版长图文计算段落的列数
        /// </summary>
        /// <param name="strs">文本内容   传入文本段首没有空两格</param>
        /// <param name="h">图片的高度</param>
        /// <param name="font">字体</param>
        /// <param name="top">上边边距</param>
        /// <param name="bottom">下边边距</param>
        /// <returns></returns>
        public static int RowNum_H(string strs, int h, Font font, int top, int bottom)
        {
            int row = 0;     //初始行数设为0
            int slen = strs.Length;    //段落字符的总个数
            int row_H = (h - top - bottom);//一列文字可画区域的高度
            int a = font.Height;  //字体高度
            int b = row_H / a;  //一列可容纳的个数
            row = (int)Math.Ceiling((double)slen / b);
            return row;
        }

段首空格效果没做出来,失败了

 

相关标签: 长图文