引用WINDOWS API:

        [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

/// <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以上:

 /// <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);
}
}

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

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

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

 /// <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;
}

  

//测试

            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));

  

csharp: 图片字符间距的更多相关文章

  1. 使用GDI+ DrawDriverString实现行距及字符间距控制

    主要是要将一个标题和几段文字绘制到固定大小的图片上,如果一张放不下就生成多张.在使用DrawString是发现无法控制行距 namespace TibetTest {     public class ...

  2. css 字间距离_css 字体字符间距设置

    介绍下css 字间距,使用css来控制字与字之间距离,也叫css字间距方法. 使用到的css样式属性单词text-indent抬头距离,letter-spacing字与字间距. Css字间距.div ...

  3. 项目笔记---CSharp图片处理

    原文:项目笔记---CSharp图片处理 项目笔记---CSharp图片处理 最近由于项目上需要对图片进行二值化处理,就学习了相关的图片处理上的知识,从开始的二值化的意义到动态阀值检测二值化等等,并用 ...

  4. WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整

    原文:WPF文字描边的解决方法(二)--支持文字竖排和字符间距调整 自前天格式化文本效果出来后,今天又添加文本竖排和调整字符间距的功能.另外,由于上次仓促,没来得及做有些功能的设计时支持,这次也调整好 ...

  5. [Windows] 输入字符间距变宽

    今天在输入时,不会到误触到哪里,输入的字符间距变得很宽,如下图: 最后找到原因是不小心同时按下了 Shift+Space(空格),进入全角模式,就会导致输入的字符间距变宽 想要恢复,再按一次 shif ...

  6. css 字间距、CSS字体间距、css 字符间距设置

    1.text-indent设置抬头距离css缩进 2.letter-spacing来设置字与字间距_字符间距离,字体间距css样式

  7. Office2016打开doc字符间距过小

    缺少字体.........装上就行,放到windows/fonts目录下,自动安装了

  8. IDEA 在使用的过程中字符间距变大的问题

    解决办法:shift+空格半角全角快捷键

  9. 字符型图片验证码识别完整过程及Python实现

    字符型图片验证码识别完整过程及Python实现 1   摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...

随机推荐

  1. django参考博客学习

    网上发现其他人的一个django系列博客,和我学的一样是黑马的,写的挺不错的,转载学习一下 https://blog.csdn.net/u014745194/article/category/6989 ...

  2. c++之选择排序和冒泡排序实现

     1.冒泡排序 冒泡排序就是通过对比前一个和后一个数的大小,按照规则进行顺序的调换.每一轮对比之后最大或者最小值都会浮到最上面或者沉到最低下. 如:对这一数组进行冒泡排序:int a[5]{34,12 ...

  3. 考试题 T2

    题意分析 首先 要求起点终点不连通 再结合数据范围 就是最小割了 首先我们可以建一个图出来 如果\(x\)可以到\(y\)的话 那么我们就从\(x\)向\(y\)连一条代价为\(h[x]-h[y]+1 ...

  4. PHP中引入文件的四种方式详解

    四种语句 PHP中有四个加载文件的语句:include.require.include_once.require_once. 基本语法 require:require函数一般放在PHP脚本的最前面,P ...

  5. laravel框架图片上传

    1.建控制器方法 2.建立路由 绑定控制器方法 3.进行图片上传的配置 修改图片上传的路径 a) config/filesystems.php 修改disks->local->root(图 ...

  6. Ubuntu 16.04防火墙

    防火墙(ufw) 说明:简单版本的防火墙,底层依赖于iptables. 安装:sudo apt-get install ufw 查看状态:sudo ufw status 开启/关闭:sudo ufw ...

  7. BLE pairing vs. bonding

    differece between pairing and bonding .see

  8. start and end call use itelephony and how to pick up a call

    Bluetooth Headset service: 但想想而已. 没有蓝牙耳机如何调用它来接听电话.想想有点搞笑. 网上扒的通过添加一个ITelephony.aidl来反射,注意aidl的写法,如果 ...

  9. .net core webapi 使用过滤器。

    过滤器一般用于权限校验.日志处理... 一:ActionFilterAttribute过滤器. 1:建一个类,继承于ActionFilterAttribute抽象类. public class Log ...

  10. zendstudio 设置默认编码 utf-8 gbk

    1.Project > Properties > Resource 2.Window > Preferences > General > Workspace 3.Wind ...