思路:将文字生成图片,再加载此图片伸缩至需要大小。
首先要获取文字的实际大小。宽度高度,再通过图片缩放就OK了
public static void DrawText() { Font f = new Font("Aris", 15, FontStyle.Italic | FontStyle.Strikeout); string s = "我的测试"; Bitmap b = new Bitmap(1, 1); Graphics g = Graphics.FromImage(b); var sf = g.MeasureString(s, f); Bitmap b1 = new Bitmap(Convert.ToInt32(sf.Width), Convert.ToInt32(sf.Height)); Graphics g1 = Graphics.FromImage(b1); g1.Clear(Color.White); RectangleF rf = new RectangleF(0, 0, sf.Width, sf.Height); g1.DrawString(s, f, Brushes.Red, rf); System.IO.MemoryStream ms = new System.IO.MemoryStream(); b1.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); Graphics g2 = Graphics.FromImage(bmap); g2.TranslateTransform(10, 10); g2.RotateTransform(30); g2.TranslateTransform(-10, -10); g2.DrawImage(Image.FromStream(ms), new Rectangle(10, 10, 200, 200)); g2.ResetTransform(); bmap.Save(AppDomain.CurrentDomain.BaseDirectory + "123.jpg"); ms.Dispose(); }