none
Paste image to Richtextbox without lost transparency (Vb.net 2015) RRS feed

  • Question

  • Hi, thanks for your time.
    I have a simple code to add a image to Richtextbox, but it loses the transparency . I was try to paste on another target (Microsofr Paint), but the transparency is losed on the Copy event apparently. 

    My code:

                If ico.ImgSel IsNot Nothing Then
    
                    Clipboard.SetImage(ico.ImgSel)
                    rtb_msg.Paste()
                    rtb_msg.Focus()
                    rtb_msg.SelectionStart = rtb_msg.Text.Length
    
                End If

    Thanks again. Regards.

    Tuesday, August 6, 2019 11:05 AM

Answers

  • Hi,

    see the following link:

    https://stackoverflow.com/questions/55505495/richtextbox-how-change-background-of-image-ive-paste?noredirect=1&lq=1

    vb.net 

       Private Function Transparent2Color(ByVal bmp1 As Bitmap, ByVal target As Color) As Bitmap
            Dim bmp2 As Bitmap = New Bitmap(bmp1.Width, bmp1.Height)
            Dim rect As Rectangle = New Rectangle(Point.Empty, bmp1.Size)
    
            Using G As Graphics = Graphics.FromImage(bmp2)
                G.Clear(target)
                G.DrawImageUnscaledAndClipped(bmp1, rect)
            End Using
    
            Return bmp2
        End Function

    Best Regards,

    Alex


    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.

    • Marked as answer by CM16 Tuesday, August 13, 2019 11:09 AM
    Tuesday, August 13, 2019 7:59 AM

All replies

  • Hi,

    You can add a PictureBox to the rtb_msb,code:

    Public Class Form1
        Dim image1 As Image = Image.FromFile("D:\image\test.png")
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim pic As New PictureBox With {.Location = New Point(0, (rtb_msg.Font.Height) * (rtb_msg.Lines.Length)), .SizeMode = PictureBoxSizeMode.StretchImage, .Image = image1}
            rtb_msg.Controls.Add(pic)
        End Sub
    End Class
    

    Best Regards,

    Alex


    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.

    Wednesday, August 7, 2019 3:19 AM
  • Hi. Thanks for your response. I tryed it but dont work for me. I am working on a LAN chat and i nedd add the picture on de 'Message RichtTextBox' and send it travel  the LAN. Your code add a image but i can't send it. It seems that add the picture like background. I need add it like emoticon 

    Its the result of my original code

    error, transparent, picture, richtextbox

    Friday, August 9, 2019 5:18 PM
  • Hi,

    I'm not sure if I can understand your situation.
    But I'm afraid you must have forgotten specifying what the copying target is.  Is it a file, screenshot which you copied?

    Regards, 

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Friday, August 9, 2019 10:16 PM
  • yes, sorry. Is a capture on Clipboard. I'am trying to paste the picture on the richtextbox of the private message or my Lan Chat application to send to receiver (like makes Skype). It is working but the picture changes 

    example, chat, lan, image, transparency

    Saturday, August 10, 2019 11:34 AM
  • Hi,

    see the following link:

    https://stackoverflow.com/questions/55505495/richtextbox-how-change-background-of-image-ive-paste?noredirect=1&lq=1

    vb.net 

       Private Function Transparent2Color(ByVal bmp1 As Bitmap, ByVal target As Color) As Bitmap
            Dim bmp2 As Bitmap = New Bitmap(bmp1.Width, bmp1.Height)
            Dim rect As Rectangle = New Rectangle(Point.Empty, bmp1.Size)
    
            Using G As Graphics = Graphics.FromImage(bmp2)
                G.Clear(target)
                G.DrawImageUnscaledAndClipped(bmp1, rect)
            End Using
    
            Return bmp2
        End Function

    Best Regards,

    Alex


    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.

    • Marked as answer by CM16 Tuesday, August 13, 2019 11:09 AM
    Tuesday, August 13, 2019 7:59 AM
  • The WordPad utility, which is available in Windows, uses a RichTextBox too. Did you check if images, copied by your code to Clipboard, can be pasted manually correctly?

    You can also adjust your icons to have a white background instead of transparent.

    Tuesday, August 13, 2019 8:42 AM
  • Hi Alex, thanks, that works perfectly. I was searching a software to convert the background to all pictures massively, but finally, with this function,  i convert the picture before paste it.
    thanks to everibody for your help.

    Regards

    the code on VB

    Private Function Transparent2Color(ByVal bmp1 As Bitmap, ByVal target As Color) As Bitmap
            Dim bmp2 As Bitmap = New Bitmap(bmp1.Width, bmp1.Height)
            Dim rect As Rectangle = New Rectangle(Point.Empty, bmp1.Size)
    
            Using G As Graphics = Graphics.FromImage(bmp2)
                G.Clear(target)
                G.DrawImageUnscaledAndClipped(bmp1, rect)
            End Using
    
            Return bmp2
    End Function

    Tuesday, August 13, 2019 11:09 AM