1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| public static Bitmap GenTextPic(string oldText, string newText) { oldText = " " + oldText; oldText = oldText.Replace("\\n", "\n").Replace("\n", "\n "); newText = newText.Replace("\\n", "\n").Replace("\n", "\n "); int padding = 20, width = MainSave.PicWidth; Font font = new Font(MainSave.Font, 16, FontStyle.Regular); int maxWidth = width - padding * 2, charGap = -8, maxHeight = 0; using (Bitmap Result = new Bitmap(width, 30000)) { using (Graphics g = Graphics.FromImage(Result)) { g.SmoothingMode = SmoothingMode.AntiAlias; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.FillRectangle(Brushes.White, new RectangleF(0, 0, width, 30000)); PointF nowPoint = new PointF(padding, padding); float maxCharWidth = 0; foreach (var item in oldText) { DrawString(g, item, Brushes.Black, ref nowPoint, font, padding, charGap, ref maxCharWidth, maxWidth, out float charHeight); } foreach (var item in newText) { DrawString(g, item, Brushes.Red, ref nowPoint, font, padding, charGap, ref maxCharWidth, maxWidth, out float charHeight); maxHeight = (int)(nowPoint.Y + charHeight + padding); } } Bitmap tmp = new Bitmap(width, maxHeight); using (Graphics g = Graphics.FromImage(tmp)) g.DrawImageUnscaled(Result, new Point(0, 0)); return tmp; } }
public static void DrawString(Graphics g, char text, Brush color, ref PointF point, Font font, int padding, int charGap, ref float maxCharWidth, int maxWidth, out float charHeight) { var charSize = g.MeasureString(text.ToString(), font); charHeight = charSize.Height; if (text == '\n') { point.X = padding; point.Y += charSize.Height + 2; return; } if (charSize.Width < -charGap) charSize.Width = maxCharWidth > 0 ? maxCharWidth : (-charGap) * 3; maxCharWidth = Math.Max(maxCharWidth, charSize.Width); g.DrawString(text.ToString(), font, color, point); WrapTest(maxWidth, padding, charGap, charSize, ref point); } public static void WrapTest(int maxWidth, int padding, int charGap, SizeF charSize, ref PointF point) { if (point.X + charSize.Width >= maxWidth) { point.X = padding; point.Y += charSize.Height + 2; } else { point.X += charSize.Width + charGap; } }
|