C# DrawString 怎么设置字间距和行间距

时间:2021-08-28 06:08:53
我自己写了一个 方法

//自动换行文字
        private Image drawString(Image image,string text,RectangleF rectF,Font font,Brush brush) {
            if (text != string.Empty && text != "" && text != null && image!=null) {
                Graphics g = Graphics.FromImage(image);
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode = InterpolationMode.High;
                g.DrawString(text, font, brush, rectF);
                g.Dispose();
            }            
            return image;
        }

怎么才能设置 字间距和行间距?

6 个解决方案

#1



//设置字符间隔
[DllImport("gdi32.dll")]
public static extern int SetTextCharacterExtra(IntPtr hdc, int nCharExtra);  

#2



//gdi32.dll
原型:int SetTextCharacterExtra(HDC hdc, int nCharExtra);

#3


引用 2 楼 hwenycocodq520 的回复:

//gdi32.dll
原型:int SetTextCharacterExtra(HDC hdc, int nCharExtra);

能嵌入 我的方法里吗 小白一个 你这样写 我不懂

#4



[DllImport("gdi32.dll")]
public static extern int SetTextCharacterExtra(IntPtr hdc, int nCharExtra);

private Image drawString(Image image,string text,RectangleF rectF,Font font,Brush brush,int nCharExtra) 
{
    if (text != string.Empty && text != "" && text != null && image!=null) 
    {
       Graphics g = Graphics.FromImage(image);
       IntPtr hdc = g.GetHdc();
       SetTextCharacterExtra(hdc, nCharExtra);
       g.ReleaseHdc(hdc);
       g.SmoothingMode = SmoothingMode.HighQuality;
       g.CompositingQuality = CompositingQuality.HighQuality;
       g.InterpolationMode = InterpolationMode.High;
       g.DrawString(text, font, brush, rectF);
       g.Dispose();
   }            
   return image;
}

#5


引用 4 楼 hwenycocodq520 的回复:

[DllImport("gdi32.dll")]
public static extern int SetTextCharacterExtra(IntPtr hdc, int nCharExtra);

private Image drawString(Image image,string text,RectangleF rectF,Font font,Brush brush,int nCharExtra) 
{
    if (text != string.Empty && text != "" && text != null && image!=null) 
    {
       Graphics g = Graphics.FromImage(image);
       IntPtr hdc = g.GetHdc();
       SetTextCharacterExtra(hdc, nCharExtra);
       g.ReleaseHdc(hdc);
       g.SmoothingMode = SmoothingMode.HighQuality;
       g.CompositingQuality = CompositingQuality.HighQuality;
       g.InterpolationMode = InterpolationMode.High;
       g.DrawString(text, font, brush, rectF);
       g.Dispose();
   }            
   return image;
}

不知道为什么没效果

#6


引用 5 楼 h420943302 的回复:
Quote: 引用 4 楼 hwenycocodq520 的回复:


[DllImport("gdi32.dll")]
public static extern int SetTextCharacterExtra(IntPtr hdc, int nCharExtra);

private Image drawString(Image image,string text,RectangleF rectF,Font font,Brush brush,int nCharExtra) 
{
    if (text != string.Empty && text != "" && text != null && image!=null) 
    {
       Graphics g = Graphics.FromImage(image);
       IntPtr hdc = g.GetHdc();
       SetTextCharacterExtra(hdc, nCharExtra);
       g.ReleaseHdc(hdc);
       g.SmoothingMode = SmoothingMode.HighQuality;
       g.CompositingQuality = CompositingQuality.HighQuality;
       g.InterpolationMode = InterpolationMode.High;
       g.DrawString(text, font, brush, rectF);
       g.Dispose();
   }            
   return image;
}

不知道为什么没效果


因为上面的方法是.net 2.0   .net 3.5 以上 请参考下面 链接里的方法。
http://www.cnblogs.com/geovindu/p/4315410.html  

#1



//设置字符间隔
[DllImport("gdi32.dll")]
public static extern int SetTextCharacterExtra(IntPtr hdc, int nCharExtra);  

#2



//gdi32.dll
原型:int SetTextCharacterExtra(HDC hdc, int nCharExtra);

#3


引用 2 楼 hwenycocodq520 的回复:

//gdi32.dll
原型:int SetTextCharacterExtra(HDC hdc, int nCharExtra);

能嵌入 我的方法里吗 小白一个 你这样写 我不懂

#4



[DllImport("gdi32.dll")]
public static extern int SetTextCharacterExtra(IntPtr hdc, int nCharExtra);

private Image drawString(Image image,string text,RectangleF rectF,Font font,Brush brush,int nCharExtra) 
{
    if (text != string.Empty && text != "" && text != null && image!=null) 
    {
       Graphics g = Graphics.FromImage(image);
       IntPtr hdc = g.GetHdc();
       SetTextCharacterExtra(hdc, nCharExtra);
       g.ReleaseHdc(hdc);
       g.SmoothingMode = SmoothingMode.HighQuality;
       g.CompositingQuality = CompositingQuality.HighQuality;
       g.InterpolationMode = InterpolationMode.High;
       g.DrawString(text, font, brush, rectF);
       g.Dispose();
   }            
   return image;
}

#5


引用 4 楼 hwenycocodq520 的回复:

[DllImport("gdi32.dll")]
public static extern int SetTextCharacterExtra(IntPtr hdc, int nCharExtra);

private Image drawString(Image image,string text,RectangleF rectF,Font font,Brush brush,int nCharExtra) 
{
    if (text != string.Empty && text != "" && text != null && image!=null) 
    {
       Graphics g = Graphics.FromImage(image);
       IntPtr hdc = g.GetHdc();
       SetTextCharacterExtra(hdc, nCharExtra);
       g.ReleaseHdc(hdc);
       g.SmoothingMode = SmoothingMode.HighQuality;
       g.CompositingQuality = CompositingQuality.HighQuality;
       g.InterpolationMode = InterpolationMode.High;
       g.DrawString(text, font, brush, rectF);
       g.Dispose();
   }            
   return image;
}

不知道为什么没效果

#6


引用 5 楼 h420943302 的回复:
Quote: 引用 4 楼 hwenycocodq520 的回复:


[DllImport("gdi32.dll")]
public static extern int SetTextCharacterExtra(IntPtr hdc, int nCharExtra);

private Image drawString(Image image,string text,RectangleF rectF,Font font,Brush brush,int nCharExtra) 
{
    if (text != string.Empty && text != "" && text != null && image!=null) 
    {
       Graphics g = Graphics.FromImage(image);
       IntPtr hdc = g.GetHdc();
       SetTextCharacterExtra(hdc, nCharExtra);
       g.ReleaseHdc(hdc);
       g.SmoothingMode = SmoothingMode.HighQuality;
       g.CompositingQuality = CompositingQuality.HighQuality;
       g.InterpolationMode = InterpolationMode.High;
       g.DrawString(text, font, brush, rectF);
       g.Dispose();
   }            
   return image;
}

不知道为什么没效果


因为上面的方法是.net 2.0   .net 3.5 以上 请参考下面 链接里的方法。
http://www.cnblogs.com/geovindu/p/4315410.html