locked
Add image to rich text box using open file dialog RRS feed

  • Question

  • Hi,

    I would like the user to be able to select images to add into the rich textbox. I have already done the filter stuff. I am really stuck and don't know how. 

    I have seen similar threads showing how to do this but they all involve the clipboard and copy and paste. With the application I am building it is important that the user can retain his/her own clipboard information to add into the rich textbox. 

    Here is my code so far: 

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
    
        OpenFileDialog1.Filter = "png|*.png"
        OpenFileDialog1.ShowDialog()
    
        Dim strFileName As String
    
        strFileName = OpenFileDialog1.FileName
    
      End Sub

     

     

    Monday, January 3, 2011 3:09 PM

Answers

  • Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "png |*.png"
        OpenFileDialog1.Title = "Image Wizard"
        OpenFileDialog1.FileName = ""
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
          Dim img As Image = Image.FromFile(OpenFileDialog1.FileName)
          Clipboard.SetImage(img)
          Me.RichTextBox1.Paste()
        Else
          Me.RichTextBox1.Text = "No Image selected."
        End If
    
      End Sub
    


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by TusharBhatia Tuesday, January 4, 2011 2:20 AM
    Monday, January 3, 2011 6:39 PM

All replies

  • Save the clipboard before you use it to paste your image and restore it afterwards.
    • Proposed as answer by Giftzwockel Monday, January 3, 2011 6:40 PM
    Monday, January 3, 2011 6:25 PM
  • Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "png |*.png"
        OpenFileDialog1.Title = "Image Wizard"
        OpenFileDialog1.FileName = ""
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
          Dim img As Image = Image.FromFile(OpenFileDialog1.FileName)
          Clipboard.SetImage(img)
          Me.RichTextBox1.Paste()
        Else
          Me.RichTextBox1.Text = "No Image selected."
        End If
    
      End Sub
    


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by TusharBhatia Tuesday, January 4, 2011 2:20 AM
    Monday, January 3, 2011 6:39 PM
  • Or very close to your own code:

     

    
    
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        OpenFileDialog1.Filter = "png|*.png"
        OpenFileDialog1.ShowDialog()
    
        Dim strFileName As Image = Image.FromFile(OpenFileDialog1.FileName)
        Clipboard.SetImage(strFileName)
        Me.RichTextBox1.Paste()
    
      End Sub

    Note the declaration for strFileName is as Image and not a String!!

     


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    Monday, January 3, 2011 6:49 PM
  • Hi TusharBhatia

    "If you've searched the Internet for an easy way to insert images into a RichTextBox, chances are you've come across variations of the
        following solution which copies the image to the clipboard, pastes it in the target RichTextBox, and clears the contents of the clipboard. 
      

        The solution must:

        1.Allow plain text to be programmatically inserted into or appended to the content if a RichTextBox at runtime.
        2.Allow the font, text color, and highlight color (background color of the text) to be specified when inserting or appending plain text to the content of a RichTextBox.
        3.Allow images to be inserted programmatically without the use of the clipboard. "

        This article is dated Jul/15/2003
        http://www.codeproject.com/KB/edit/csexrichtextbox.aspx

     


    Be a good forum member. Make this forum a great place to meet and interact with others around the world.
    Monday, January 3, 2011 11:28 PM
  • I used the above code for my application, with a little bit of modifications. 

    Thank you for your response, it was greatly appreciated. 

    Tuesday, January 4, 2011 2:23 AM