richTextBox.DrawToBitmap不会绘制包含文本?

如果我有一个richTextBox并在其上运行DrawToBitmap,它不会绘制richTextBox内的任何文本。

Bitmap b = new Bitmap(rtb.Width, rtb.Height); inputControl.DrawToBitmap(b, new Rectangle(0, 0, b.Width, b.Height)); 

有没有什么办法解决这一问题?

这个post在Google中名列第二。 似乎有你想要的。 因为我想你在这个问题中使用了这个问题, 接受表单元素作为方法参数? ,最好做这样的事情。

 if(inputControl is RichTextBox) { //do specifc magic here } else { //general case } 

您可以递归检查包含RichTextBox的Control

 bool ContainsOrIsRichTextBox(Control inputControl) { if(inputControl is RichTextBox) return true; foreach(Control control in inputControl.Controls) { if(ContainsOrIsRichTextBox(control)) return true; } return false; } 

我没有编译这个,并且有一种方法可以在不冒StackOverflowException的情况下完成它,但是这应该让你开始。

我知道这是相对陈旧的,但我在http://www.windows-tech.info/3/8ffaf21eed5de2d4.php找到了一个有效的解决方案:

 public static Bitmap RtbToBitmap(RichTextBox rtb) { rtb.Update(); // Ensure RTB fully painted Bitmap bmp = new Bitmap(rtb.Width, rtb.Height); using (Graphics gr = Graphics.FromImage(bmp)) { gr.CopyFromScreen(rtb.PointToScreen(Point.Empty), Point.Empty, rtb.Size); } return bmp; } 

从RichTextBox.DrawToBitmap()的MSDN Library文章:

此方法与此类无关。

一种令人讨厌的方式,说本机Windows richedit控件不支持WM_PRINT。 拍摄屏幕截图是一种选择,诺维科夫给你一个链接到我的答案。

我在这里找到了一个相关的答案: 如何在任何设备contenxt上打印富文本框内容并使用正确的格式?

我更改了这个以将我的屏幕外RichTextBox渲染为位图。 这样我就可以在屏幕外创建一个位图,然后将其发送到OpenGL。

  // Convert the unit used by the .NET framework (1/100 inch) // and the unit used by Win32 API calls (twips 1/1440 inch) private const double anInch = 14.4; [StructLayout(LayoutKind.Sequential)] private struct RECT { public int Left; public int Top; public int Right; public int Bottom; } [StructLayout(LayoutKind.Sequential)] private struct CHARRANGE { public int cpMin; // First character of range (0 for start of doc) public int cpMax; // Last character of range (-1 for end of doc) } [StructLayout(LayoutKind.Sequential)] private struct FORMATRANGE { public IntPtr hdc; // Actual DC to draw on public IntPtr hdcTarget; // Target DC for determining text formatting public RECT rc; // Region of the DC to draw to (in twips) public RECT rcPage; // Region of the whole DC (page size) (in twips) public CHARRANGE chrg; // Range of text to draw (see earlier declaration) } private const int WM_USER = 0x0400; private const int EM_FORMATRANGE = WM_USER + 57; [DllImport("USER32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); ///  /// Render the specified RichTextBox onto the specified bitmap ///  /// RichTextBox to render /// Bitmap to render the RichTextBox onto public void RenderToBitmap(RichTextBox textBox, Bitmap bitmap) { // Set area to render to be entire bitmap RECT rect; rect.Left = 0; rect.Top = 0; rect.Right = (int)(bitmap.Width * anInch); rect.Bottom = (int)(bitmap.Height * anInch); Graphics g = Graphics.FromImage(bitmap); IntPtr hdc = g.GetHdc(); FORMATRANGE fmtRange; fmtRange.chrg.cpMin = textBox.GetCharIndexFromPosition(new Point(0,0)); fmtRange.chrg.cpMax = textBox.GetCharIndexFromPosition(new Point(bitmap.Width,bitmap.Height)); fmtRange.hdc = hdc; // Use the same DC for measuring and rendering fmtRange.hdcTarget = hdc; fmtRange.rc = rect; fmtRange.rcPage = rect; IntPtr lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange)); Marshal.StructureToPtr(fmtRange, lparam, false); // Render the control to the bitmap SendMessage(textBox.Handle, EM_FORMATRANGE, new IntPtr(1), lparam); // Clean up Marshal.FreeCoTaskMem(lparam); g.ReleaseHdc(hdc); } 

对于它的价值,RichTextBox控件的更高版本正确支持DrawToBitmap方法; 它还可以提高性能并具有更多function。

 internal class RichTextBox5: RichTextBox { [DllImport("kernel32.dll", CharSet = CharSet.Auto)] static extern IntPtr LoadLibrary(string lpFileName); protected override CreateParams CreateParams { get { CreateParams cparams = base.CreateParams; if (LoadLibrary("msftedit.dll") != IntPtr.Zero) { cparams.ClassName = "RICHEDIT50W"; } return cparams; } } }