如果我有一个richTextBox并在其上运行DrawToBitmap,它不会在richTextBox内部绘制任何文本。
Bitmap b = new Bitmap(rtb.Width, rtb.Height);
inputControl.DrawToBitmap(b, new Rectangle(0, 0, b.Width, b.Height));
有什么方法可以解决这个问题吗?
答案 0 :(得分:4)
我知道这是相对陈旧的,但我在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;
}
答案 1 :(得分:3)
来自RichTextBox.DrawToBitmap()的MSDN Library文章:
此方法与此课程无关。
一种糟糕的方式,表示本机Windows richedit控件不支持WM_PRINT。拍摄屏幕截图是一种选择,诺维科夫给了你一个回答链接。
答案 2 :(得分:2)
This thread在谷歌排名第二。似乎完全符合你的要求。因为我想你在这个问题Accepting Form Elements As Method Arguments?中使用了这个函数,所以最好做这样的事情。
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风险的情况下完成它,但这应该让你开始。
答案 3 :(得分:2)
对于它的价值,RichTextBox控件的更高版本正确支持DrawToBitmap方法;它还可以提高性能并具有更多功能。
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;
}
}
}
答案 4 :(得分:1)
我在这里找到了一个相关的答案:how to print Rich text box contents on any device contenxt with proper formatting?
我更改了这个以将我的屏幕外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);
/// <summary>
/// Render the specified RichTextBox onto the specified bitmap
/// </summary>
/// <param name="textBox">RichTextBox to render</param>
/// <param name="bitmap">Bitmap to render the RichTextBox onto</param>
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);
}
答案 5 :(得分:0)
我测试了上面的方法,每当我将保存的位图加载到ImageViewer(如Paint)中时,SaveFileDialog-UI都会淡入文本背景。幸运的是,我找到了一个简单的解决方法:
SaveFileDialog bfsd = new SaveFileDialog();
var rtb = richTextBox1;
bfsd.Filter = "Bitmap (*.bmp)|*.bmp|All Files (*.*)|*.*";
bfsd.Title = "Save your text as a Bitmap File";
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);
}
if (bfsd.ShowDialog()==DialogResult.OK)
{
try
{
bmp.Save(bfsd.FileName);
bmp.Dispose();
}
catch (Exception)
{
DialogResult dr = MessageBox.Show("An error ocurred while attempting to save your Image...", "Error! Error!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dr == DialogResult.Retry)
{
drawToBitmapbmpToolStripMenuItem_Click(sender, e);
}
else if (dr == DialogResult.Cancel)
{
return;
}
}
Save
之前就绘制图片(请放心,直到您按Save
才真正保存图像)< / li>
按下Cancel
不会影响该过程,因为当您按下Button
或MenuStripItem
进行保存时,它将更新并重新绘制:)
我实现了一种try
-catch
方法,因此如果发生错误,它将捕获错误,而不是仅应用(Not Responding)
catch
方法是Retry
Button
它将捕获错误,并让您选择Cancel
整个Operation
或Retry
希望对您有所帮助:)