如何通过编码自动将图像打印并将其插入到RichTextBox控件中 [英] How to darw a image and insert it into a RichTextBox control automatically by coding
问题描述
嗨。
我想自动生成一份文件。该文件应包含一些图像。
我首先要通过编码绘制所有图像,而不是从外部文件导入图像。但是这个目标有点难。所以我想导入一个外部图像文件,只添加一些文本。然后我想将这个图像插入到RichTextBox中?
这是我的工作:
我创建一个新的表单项目并添加1 Picturebox,1个RichTextBox和3个按钮(btnGen生成文档,btnSave将文档保存为.rtf文件,btnImport导入外部图像文件)。
这是导入外部的功能图片文件到我的PictureBox。
私人 Sub ImportPicture( ByVal PictureName As String , ByRef PicBox As PictureBox)
尝试
Dim img As Image = Image。 FromFile(PictureName)
PicBox.Image = img
Catch ex As 异常
Console.WriteLine(vbCrLf + ex.StackTrace + vbCrLf + ex.Message)
< span class =code-keyword>结束 尝试
结束 Sub
双击btnImport按钮。现在我要导入外部文件,然后我想在其上绘制一些单词。这是我不能做的。
私人 Sub btnImport_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs)句柄 btnImport.Click
' 首先导入图像
< span class =code-keyword> Dim ImagePath As String = My.Application。 Info.DirectoryPath + \sample.png
ImportPicture(ImagePath,我 .PictureBox1)
' ------ ------------现在,如何添加一些文字到图像?----------------------------
结束 Sub
双击btnGen按钮自动生成文档。 />
私有 Sub btnGen_Click( ByVal 发件人作为系统。对象, ByVal e As System.EventArgs)句柄 btnGen.Click
Me .WindowState = FormWindowState.Maximized
Me .RichTextBox1.SetBounds( 15 , 50 ,我 .Width - 45 ,我 .Height - 90 )
我 .PictureBox1.Visible = 错误
GenerateDoc( Me .RichTextBox1)
结束 Sub
这是将文字和图像添加到doucment的两个功能。
私人 Sub GenerateDoc(< span class =code-keyword> ByRef RtfBox As RichTextBox)
RtfBox.SelectionAlignment = HorizontalAlignment.Center
RtfBox.SelectedText = Tittle + vbCrLf
RtfBox.SelectionAlignment = HorizontalAlignment.Left
RtfBox .SelectedText = 第一个文本 + vbCrLf
InsertPic2Doc( Me .PictureBox1)
RtfBox.SelectedText = vbCrLf
RtfBox.SelectedText = 这是文本的第二行 + vbCrLf
RtfBox.SelectionAlignment = HorizontalAlignment.Center
InsertPic2Doc(我 .PictureBox1)' InsertPicture(Me.pBox)
RtfBox.SelectedText = vbCrLf
RtfBox.SelectedText = Fig.2 + vbCrLf
RtfBox.SelectionAlignment = HorizontalAlignment.Left
RtfBox.SelectedText = 这是第三行
结束 Sub
私人 Sub InsertPic2Doc( ByRef PicBox As PictureBox)
Clipboard.Clear()
Clipboard.SetImage(PicBox.Image)
RichTextBox1.Paste()
结束 Sub
基本上,富文本格式非常陈旧,几乎已过时且笨拙使用。由于这个或其他原因,现有的API(包括原始Windows API)不包括格式支持的所有操作。因此,在一般情况下,您需要学习格式本身并从低级别开始工作。格式本身完全可用:
http://en.wikipedia.org/wiki/Rich_Text_Format [ ^ ],
< a href =http://www.microsoft.com/en-us/download/details.aspx?id=10725> http://www.microsoft.com/en-us/download/details.aspx?id = 10725 [ ^ ]。
请参阅此CodeProject文章:在运行时将纯文本和图像插入RichTextBox [ ^ ]。
此外,有一个简单的黑客,我不能认为是正确的,因为它是intru因为它修改了系统剪贴板的内容:您可以将图像放在剪贴板中并将其放在富文本中。例如,见:
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/36a6dac3-f14e-4d8f-b376-246bb6a56717/ [ ^ ]。
或者,我建议调查其他格式。一个不错的选择是HTML,但如何在System.Window.Forms
中呈现它?如何插入图形而不使用外部文件?这很有可能。 CodeProject的这篇文章提供了质量非常好的解决方案:您将使用的专业HTML渲染器 [ ^ ]。
顺便说一下,这个组件内部使用... RTF。因此,您可以以完全不同的方式使用它:查看其实现并学习如何执行所有RTF技巧。
-SA
Hi.
I want to generate a document automatically. The document should contain some images.
I first want to draw a image all by coding, not import a image from an external file. But that goal was a little hard. So I want to import a external image file, and only add some text to it. Then I want to insert this image into a RichTextBox?
Here is what I do:
I create a new form project and add 1 Picturebox, 1 RichTextBox and 3 buttons(btnGen to generate the document, btnSave to save the document as a .rtf file and btnImport to import a external image file).
This is the function to import a external image file to my PictureBox.
Private Sub ImportPicture(ByVal PictureName As String, ByRef PicBox As PictureBox)
Try
Dim img As Image = Image.FromFile(PictureName)
PicBox.Image = img
Catch ex As Exception
Console.WriteLine(vbCrLf + ex.StackTrace + vbCrLf + ex.Message)
End Try
End Sub
Double click the btnImport button. Now I want to import the external file, and then I want to draw some words on it. Here is what I can not do.
Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
'Import the image first
Dim ImagePath As String = My.Application.Info.DirectoryPath + "\sample.png"
ImportPicture(ImagePath, Me.PictureBox1)
'------------------Now, how to add some words to the image?----------------------------
End Sub
Double click the btnGen button to generate the document automatically.
Private Sub btnGen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGen.Click
Me.WindowState = FormWindowState.Maximized
Me.RichTextBox1.SetBounds(15, 50, Me.Width - 45, Me.Height - 90)
Me.PictureBox1.Visible = False
GenerateDoc(Me.RichTextBox1)
End Sub
And these are the two funtions to add text and image to the doucment.
Private Sub GenerateDoc(ByRef RtfBox As RichTextBox)
RtfBox.SelectionAlignment = HorizontalAlignment.Center
RtfBox.SelectedText = "Tittle" + vbCrLf
RtfBox.SelectionAlignment = HorizontalAlignment.Left
RtfBox.SelectedText = "First texts" + vbCrLf
InsertPic2Doc(Me.PictureBox1)
RtfBox.SelectedText = vbCrLf
RtfBox.SelectedText = "This is the second line of the texts" + vbCrLf
RtfBox.SelectionAlignment = HorizontalAlignment.Center
InsertPic2Doc(Me.PictureBox1) ' InsertPicture(Me.pBox)
RtfBox.SelectedText = vbCrLf
RtfBox.SelectedText = "Fig.2" + vbCrLf
RtfBox.SelectionAlignment = HorizontalAlignment.Left
RtfBox.SelectedText = "This is the third line"
End Sub
Private Sub InsertPic2Doc(ByRef PicBox As PictureBox)
Clipboard.Clear()
Clipboard.SetImage(PicBox.Image)
RichTextBox1.Paste()
End Sub
Basically, rich text format is very old, pretty much obsolete and awkward to use. By this or other reasons, existing APIs, including raw Windows API, do not include all the operations the format support. So, in general case, you would need to learn the format itself and work from the low level. The format itself is fully available:
http://en.wikipedia.org/wiki/Rich_Text_Format[^],
http://www.microsoft.com/en-us/download/details.aspx?id=10725[^].
Please see this CodeProject article: Insert Plain Text and Images into RichTextBox at Runtime[^].
Besides, there is one simple hack which I cannot consider quite correct because it is intrusive as it modified the content of the system clipboard: you can put image in the clipboard and past it in the rich text. See, for example:
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/36a6dac3-f14e-4d8f-b376-246bb6a56717/[^].
Alternatively, I would advice to look into other formats. One good option would be HTML, but how to render it inSystem.Window.Forms
? How to insert graphics without using external files? This is quite possible. This CodeProject article offers a solution of amazingly good quality: A Professional HTML Renderer You Will Use[^].
By the way, internally this component uses… RTF. So, you can use it in a completely different way: look at its implementation and learn how to do all the RTF tricks.
—SA
这篇关于如何通过编码自动将图像打印并将其插入到RichTextBox控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!