csharp: 图片字符间距
引用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: 图片字符间距的更多相关文章
- 使用GDI+ DrawDriverString实现行距及字符间距控制
主要是要将一个标题和几段文字绘制到固定大小的图片上,如果一张放不下就生成多张.在使用DrawString是发现无法控制行距 namespace TibetTest { public class ...
- css 字间距离_css 字体字符间距设置
介绍下css 字间距,使用css来控制字与字之间距离,也叫css字间距方法. 使用到的css样式属性单词text-indent抬头距离,letter-spacing字与字间距. Css字间距.div ...
- 项目笔记---CSharp图片处理
原文:项目笔记---CSharp图片处理 项目笔记---CSharp图片处理 最近由于项目上需要对图片进行二值化处理,就学习了相关的图片处理上的知识,从开始的二值化的意义到动态阀值检测二值化等等,并用 ...
- WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整
原文:WPF文字描边的解决方法(二)--支持文字竖排和字符间距调整 自前天格式化文本效果出来后,今天又添加文本竖排和调整字符间距的功能.另外,由于上次仓促,没来得及做有些功能的设计时支持,这次也调整好 ...
- [Windows] 输入字符间距变宽
今天在输入时,不会到误触到哪里,输入的字符间距变得很宽,如下图: 最后找到原因是不小心同时按下了 Shift+Space(空格),进入全角模式,就会导致输入的字符间距变宽 想要恢复,再按一次 shif ...
- css 字间距、CSS字体间距、css 字符间距设置
1.text-indent设置抬头距离css缩进 2.letter-spacing来设置字与字间距_字符间距离,字体间距css样式
- Office2016打开doc字符间距过小
缺少字体.........装上就行,放到windows/fonts目录下,自动安装了
- IDEA 在使用的过程中字符间距变大的问题
解决办法:shift+空格半角全角快捷键
- 字符型图片验证码识别完整过程及Python实现
字符型图片验证码识别完整过程及Python实现 1 摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...
随机推荐
- CentOS6.5下samba服务
为减少错误已提前关掉了SELinux,防火墙. 安装rpm包: samba-3.6.9-164.el6.x86_64.rpm 启动检测:samba服务可以正常启动!(证明RPM安装正常) 配置文件位置 ...
- 3.3 PXC Strict Mode
摘要: 出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该 ...
- npm install 报错: WARN checkPermissions Missing write access to 解决方案
经过各种百度搜索,发现这个问题的出现并不是管理员权限的问题,而是之前安装失败了,这个文件已经存在了,再次安装无法覆盖写入的问题. 解决方法: 1.找到node的全局安装路径,一般在nodejs文件夹的 ...
- 区间DP的学习(持续更新)
例题: 1.Multiplication Puzzle 原题地址:http://poj.org/problem?id=1651 2.Dire Wolf 原题地址:http://acm.split.hd ...
- symfony4 404页面未找到
symfony4 404页面未找到 安装好symfony4后,发现除了首页能正常显示,其他页面如/_profiler_/等其他创建的router访问都显示The requested URL /xxx ...
- How to deal with the problem '<' in OpenERP's view file
In this case,if you write some stirng in your fields which contains '<' , OpenERP will give a err ...
- shiro学习笔记_0600_自定义realm实现授权
博客shiro学习笔记_0400_自定义Realm实现身份认证 介绍了认证,这里介绍授权. 1,仅仅通过配置文件来指定权限不够灵活且不方便.在实际的应用中大多数情况下都是将用户信息,角色信息,权限信息 ...
- ubuntu手动安装PhantomJS
1.切换到主目录:cd ~2.下载安装包:https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-Linux-x86_64.ta ...
- Chapter 14. Blocks and Statements
14.5. Statements There are many kinds of statements in the Java programming language. Most correspon ...
- hibernate 中addScalar的用法与作用
作用: 1.提高性能 2.指定要返回哪几个字段,为指定的不返回(主要用于select *查询全部) 3.也可指定返回字段的具体类型 常用于自定义本地sql中 如: StringBuffer sql=n ...