绘制不同颜色的文本图片

        private Bitmap strToImage(string str)

        {

            Font f = new Font(new FontFamily("宋体"), 10, FontStyle.Regular);

            float fontSizeWidth = 96 / (72 / f.Size); // 字体实际像素宽度   

            float fontSizeHeight = 96 / (72 / f.Size); // 字体实际像素高度   

           



            //创建此大小的图片

            Bitmap bmp = new Bitmap(str.Length * (int)(fontSizeWidth), 22);

            Graphics g = Graphics.FromImage(bmp);

            int fIndex= str.IndexOf(findText);

            string startStr = str.Substring(0, fIndex);

          

            float y= (23- (int)fontSizeHeight) / 2;

            g.DrawString(startStr, f, new SolidBrush(System.Drawing.Color.Black), new PointF(fontSizeWidth / 2,y));

            float startLenth = startStr.Length * (int)(fontSizeWidth );

            g.DrawString(findText, f, new SolidBrush(System.Drawing.Color.Red), new PointF(startLenth, y));//绘制红字

            string enStr = str.Substring(fIndex + findText.Length);

            float findTxLenth = findText.Length * (int)(fontSizeWidth );

            g.DrawString(enStr, f, new SolidBrush(System.Drawing.Color.Black), new PointF(startLenth+findTxLenth , y));

            g.Save();

            g.Dispose();

            return bmp;

        }

 

你可能感兴趣的:(图片)