• c# GDI+绘制不同字体的字符串


    一段字符串中可能既有汉字又有字母,对于汉字和字母分别采用不同的字体进行绘制直接po代码了

     Bitmap bmp = new Bitmap(iWidth, iHeight);
                Graphics g = Graphics.FromImage(bmp);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.FillRectangle(new SolidBrush(Color.FromArgb(0, 90, 169)), new Rectangle(0, 0, bmp.Width, bmp.Height));
                if (IFYY)
                {
                    Font arial = new System.Drawing.Font("Arial", 46F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));
                    StringFormat test = StringFormat.GenericTypographic;
                    SizeF size = g.MeasureString(strText, font, 1062, test);
                    float fX = (1062 - size.Width) / 2 + 348;
                    float fY = (58 - size.Height) / 2 + 960;
                    float rowSpace = 0f;
                    for (int i = 0; i < strText.Length; i++)
                    {
                        string ss = strText[i].ToString();
                        if (ISLetter(strText[i]))
                        {
                            SizeF sf = g.MeasureString(ss, arial);
                            g.DrawString(ss, arial, brushWhite, fX, fY);
                            if (ss == " ")
                            {
                                fX += g.MeasureString(ss, arial).Width + rowSpace;
                            }
                            else
                            {
                                fX += g.MeasureString(ss, arial, 100, test).Width + rowSpace;
                            }
                            //g.DrawString(strText[i].ToString(), arial, brushWhite, rectF, format);
                        }
                        else
                        {
                            g.DrawString(ss, font, brushWhite, fX, fY);
                            fX += g.MeasureString(ss, font, 100, test).Width + rowSpace;
                        }
                        //fX += rowSpace;
                    }
                }
                else
                {
                    g.DrawString(strText, font, brushWhite, rectF, format);
                }
                
                //g.DrawString(strText, arial, brushWhite, rectF, format);
                g.Dispose();
                bmp.RotateFlip(RotateFlipType.Rotate180FlipX);
                return bmp;
            }
    
            private static bool ISLetter(char c)
            {
                bool letter = false;
                if (c > 127)
                {
                    letter = false;
                }
                else
                {
                    letter = true;
                }
                return letter;
            }
    

     需要注意的是:

           StringFormat test = StringFormat.GenericTypographic;
                    SizeF size = g.MeasureString(strText, font, 1062, test);
    Graphics.MeasureString()方法,一定要使用如图的形式,否则测算出来的字母宽度不精确
  • 相关阅读:
    start pyhton project(2)
    java.lang.ClassFormatError: Truncated class file
    linux 查看计算机信息命令
    VS2010UltimTrialCHS 版注册码
    VS2008打包安装程序,实现覆盖安装设置
    WPF移动不规则渐变色窗体
    C#下移动无边框窗体(直接粘贴可用)
    TCP通信过程中时时监测连接是否已断开
    WIN7下使用DotNetBar,关闭Aero效果,使用Office2007Form皮肤
    【原创】企业级工作流管理系统评价依据或标准
  • 原文地址:https://www.cnblogs.com/niuge/p/4065132.html
Copyright © 2020-2023  润新知