private Image GetImageForTextBox(FastColoredTextBox box) { FastColoredTextBox boxClone = new FastColoredTextBox(); var maxLineLength = box.Lines.Max((line) => line.Length); boxClone.ShowLineNumbers = box.ShowLineNumbers; boxClone.ShowScrollBars = false; boxClone.Language = Language.QTP; boxClone.Text = box.Text; boxClone.Width = (int)(maxLineLength * 1.25 * box.CharWidth); boxClone.Height = box.TextHeight + box.Lines.Count + 10; int width, height; width = boxClone.Width; height = boxClone.Height; Bitmap bp = new Bitmap(width + 10, height); Graphics g = Graphics.FromImage(bp); g.Clear(box.BackColor); g.Flush(); g.Dispose(); try { boxClone.DrawToBitmap(bp, new Rectangle(10, 0, width + 10, height)); } catch (Exception) { bp.Dispose(); return(null); } finally { boxClone.Dispose(); } return(bp); }