none
How to copy image from Richtextbox (WinForms)

    Question

  • richtextbox.copy() is only useful in app that can read rtf, just like word, wordpad, but can't paste into Excel.

    thanks


    vb.net


    • Edited by KfCalf Friday, February 09, 2018 10:48 AM tks
    Friday, February 09, 2018 10:47 AM

All replies

  • To enable us to help you better, you need to provide as many details as you can about the problem you are experiencing. 

    Ashidacchi

    Sunday, February 11, 2018 4:34 AM
  • Hi KfCalf,

    According to your description, you want to copy image from RichTextBox, but you don't say where you want to paste it. I do one sample that copy image from RichTextBox and paste it into PictureBox, please take  look:

     Private Sub FrmRichTextBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            InsertImageInRichTextBox()
        End Sub
    
        Private Sub InsertImageInRichTextBox()
            RichTextBox1.Text = vbCrLf & "Copy an image from Richtextbox and paste it in a Picturebox :)" & vbCrLf
            Dim img As Image = Image.FromFile("D:\Image\1.jpg")
            Clipboard.SetDataObject(img)
            Dim dataFormat As DataFormats.Format = DataFormats.GetFormat(DataFormats.Bitmap)
            If RichTextBox1.CanPaste(dataFormat) Then
                RichTextBox1.Paste(dataFormat)
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Clipboard.Clear()
            Me.RichTextBox1.[Select](0, 1)
            If (Me.RichTextBox1.SelectionType And RichTextBoxSelectionTypes.Object) = RichTextBoxSelectionTypes.Object Then
                RichTextBox1.Copy()
            End If
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim idata As IDataObject = Clipboard.GetDataObject()
            If idata.GetDataPresent("System.Drawing.Bitmap") Then
                Dim imgObject As Object = idata.GetData("System.Drawing.Bitmap")
                If imgObject IsNot Nothing Then
                    Dim img As Image = TryCast(imgObject, Image)
                    If img IsNot Nothing Then
                        PictureBox1.Image = img
                    End If
                End If
            End If
        End Sub

    If my post  does not answer your doubts, please clarify your point.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, February 12, 2018 2:39 AM
    Moderator