Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
Posted 12-Apr-13 23:30pm
Edited 14-Apr-13 3:13am
v5

1 solution

Rate this: bad
good
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:
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
  Permalink  
v2
Comments
Espen Harlinn at 14-Apr-13 16:33pm
   
5'ed!
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 210
3 Afzaal Ahmad Zeeshan 204
4 CPallini 185
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 14 Apr 2013
Copyright © CodeProject, 1999-2014
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