Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

VB
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.
VB
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.
VB
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.

VB
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
Posted
Updated 14-Apr-13 2:13am
v5

1 solution

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 in System.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
 
Share this answer
 
v2
Comments
Espen Harlinn 14-Apr-13 16:33pm    
5'ed!
Sergey Alexandrovich Kryukov 14-Apr-13 16:34pm    
Thank you, Espen.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900