csharp: 图片字符间距

引用WINDOWS API:

1
2
3
4
5
6
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern int SetTextCharacterExtra(IntPtr hdc, int nCharExtra);//图片字符间距
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr handle);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);

 .NET 2.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="fontSpace"></param>
        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Font font = new Font("宋体", 12.0F);
 
            Brush brush = Brushes.Red;
            string text2 = "涂聚文";
            IntPtr hdc = e.Graphics.GetHdc();
            SetTextCharacterExtra(hdc, 16); // 设置字符间距  
            e.Graphics.ReleaseHdc(hdc);
            e.Graphics.DrawString(text2, font, brush, 20, 25);//
 
            //pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
        }

 .NET 3.5以上:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/// <summary>
       /// 图片的字符间距(只对中文,数字,字母,符号有效,在中文环境下,中文日文混排无效)
       /// 涂聚文
       /// .net3.0以上
       /// </summary>
       /// <param name="width">图片宽度</param>
       /// <param name="height">图片高度</param>
       /// <param name="space">字间距</param>
       /// <param name="strtext">要显示的文字</param>
       /// <returns>图片</returns>
       Bitmap CreateImageString(int width, int height,int space ,string strtext)
       {
           Bitmap image = new Bitmap(width, height);
           using (Graphics g = Graphics.FromImage(image))
           {
               //绘制图片边框
               //g.DrawRectangle(Pens.Black, 0, 0, width - 1, height - 1);
 
               Font font = new Font("宋体", 12.0F);
 
               Brush brush = Brushes.Red;
               //绘制设置了字符间距的输出
               DrawStringExtra(g, space, x =>
               {
                   x.DrawString(strtext, font, brush, 0, 2);
               });
           }
           return image;
       }
 
 
       /// <summary>
       ///
       /// </summary>
       /// <param name="g"></param>
       /// <param name="nCharExtra"></param>
       /// <param name="action"></param>
       void DrawStringExtra(Graphics g, int nCharExtra, Action<Graphics> action)
       {
           IntPtr hdc = g.GetHdc();
           SetTextCharacterExtra(hdc, nCharExtra);
           try
           {
               using (Graphics g1 = Graphics.FromHdc(hdc))
               {
                   action(g1);
               }
           }
           finally
           {
               SetTextCharacterExtra(hdc, 0);
               g.ReleaseHdc(hdc);
           }
       }

 调用:(可以应用于打印中)

1
pictureBox1.Image = CreateImageStrin(100, 30, 10, "3315000");

 .net 2.0自写填空格来设置字间距,高版本有:Padding,PadRight,PadLeft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/// <summary>
       /// 字符填充空格,而从设置字符间距
       /// 涂聚文
       /// </summary>
       /// <param name="str"></param>
       /// <returns></returns>
       public string SetPadstring(string str,int padwidth)
       {
           string s = string.Empty;
           string m = string.Empty;
           if (str.Length > 0)
           {
               char[] arr = str.ToCharArray();
               foreach (char d in arr)
               {
                   //MessageBox.Show(d.ToString());
                   m = m + d + PadRight(padwidth);
               }
           }
           s = m;
           return s;
       }
 
 
       /// <summary>
       /// 填充空格
       /// </summary>
       /// <param name="totalWidth"></param>
       /// <returns></returns>
       public string PadLeft(int totalWidth)
       {  
               string s="";
 
               if (totalWidth > 0)
               {
                   for (int i = 0; i < totalWidth; i++)
                   {
                       s =" "+ s  ;
                   }
               }
               return s;
      }
 
       /// <summary>
      /// 填充空格
       /// </summary>
       /// <param name="TotalWidth"></param>
       /// <returns></returns>
       public string PadRight(int TotalWidth)
       {
           string s = "";
           if (TotalWidth > 0)
           {
               for (int i = 0; i < TotalWidth; i++)
               {
                   s = s + " ";
               }
           }
 
           return s;
       }

  

1
2
3
4
5
6
7
8
9
10
11
//测试
             
            string cs = "331500涂聚文";
            //char[] arr = cs.ToCharArray();
            //foreach (char d in arr)
            //{
            //    //MessageBox.Show(d.ToString());
            //    m = m + d+PadRight(1);
            //}
            //MessageBox.Show(m);
            MessageBox.Show(SetPadstring(cs, 1));

  

 

posted @   ®Geovin Du Dream Park™  阅读(3964)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2013-03-05 css:文本两端对齐
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示