Click here to Skip to main content
13,043,898 members (47,506 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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)
            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
    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
       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)
   End Sub
Posted 12-Apr-13 22:30pm
Updated 14-Apr-13 2:13am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:[^],[^].

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:[^].

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.

Espen Harlinn 14-Apr-13 16:33pm
Sergey Alexandrovich Kryukov 14-Apr-13 16:34pm
Thank you, Espen.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 14 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100