如何为图像渲染富文本?

时间:2022-01-04 15:49:53

I'm trying to create a rich text DataGridViewCell. I can host a RichTextBox as the editing control, but when the cell isn't in editing mode I need to paint the rich text myself. I don't want to parse the text - I just need an equivalent of Graphics.DrawString that works with rich text.

我正在尝试创建一个富文本DataGridViewCell。我可以托管RichTextBox作为编辑控件,但是当单元格不处于编辑模式时,我需要自己绘制富文本。我不想解析文本 - 我只需要一个与富文本一起使用的Graphics.DrawString。

2 个解决方案

#1


Here's an example where you can add a print feature to the richtextbox. It's basically just copying the rich text to the Graphics object used for printing. I think you could modify this to use your own graphics object instead, and basically "print" to an image.

这是一个可以在richtextbox中添加打印功能的示例。它基本上只是将富文本复制到用于打印的Graphics对象。我认为您可以修改它以使用您自己的图形对象,并基本上“打印”到图像。

Edit:

Here's a post where someone seems to have modified the print code for creating an image.

这是一篇帖子,有人似乎修改了用于创建图像的打印代码。

#2


I don't believe there's a solution within WinForms that can do that for you. The best approach here is probably to use the RichTextBox for both plain rendering and editing, and just lock it (set Locked to true) when not in edit mode.

我不相信WinForms中有一个可以为你做到这一点的解决方案。这里最好的方法可能是使用RichTextBox进行普通渲染和编辑,并在不处于编辑模式时将其锁定(将Locked设置为true)。

#1


Here's an example where you can add a print feature to the richtextbox. It's basically just copying the rich text to the Graphics object used for printing. I think you could modify this to use your own graphics object instead, and basically "print" to an image.

这是一个可以在richtextbox中添加打印功能的示例。它基本上只是将富文本复制到用于打印的Graphics对象。我认为您可以修改它以使用您自己的图形对象,并基本上“打印”到图像。

Edit:

Here's a post where someone seems to have modified the print code for creating an image.

这是一篇帖子,有人似乎修改了用于创建图像的打印代码。

#2


I don't believe there's a solution within WinForms that can do that for you. The best approach here is probably to use the RichTextBox for both plain rendering and editing, and just lock it (set Locked to true) when not in edit mode.

我不相信WinForms中有一个可以为你做到这一点的解决方案。这里最好的方法可能是使用RichTextBox进行普通渲染和编辑,并在不处于编辑模式时将其锁定(将Locked设置为true)。