Total Pageviews

Wednesday, November 9, 2016

Insert Picture in RichTextBox in Visual Basic 2015

Insert Picture in RichTextBox in Visual Basic 2015
Insert Picture in RichTextBox in Visual Basic 2015
Inserting picture in RichTextBox in Visual Basic 2015 is very easy task. In this blog post I share a source code that helps you to insert different type of pictures.

        Try
            Dim GetPicture As New OpenFileDialog
            GetPicture.Filter = "Bitmaps (*.bmp), GIFs (*.gif), JPEGs (*.jpg)|*.bmp;*.gif;*.jpg|Bitmaps (*.bmp)|*.bmp|GIFs (*.gif)|*.gif|JPEGs (*.jpg)|*.jpg"
            GetPicture.FilterIndex = 1
            GetPicture.InitialDirectory = "C:\"
            If GetPicture.ShowDialog = DialogResult.OK Then
                Dim SelectedPicture As String = GetPicture.FileName
                Dim Picture As Bitmap = New Bitmap(SelectedPicture)
                Clipboard.SetImage(Picture)
                Dim PictureFormat As DataFormats.Format = DataFormats.GetFormat(DataFormats.Bitmap)
                If txtNote.CanPaste(PictureFormat) Then
                    txtNote.Paste(PictureFormat)
                End If
            End If
        Catch ex As Exception
            'The only exception that can occurs if the richtextbox 
            'not supports the picture format
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        End Try

You see, you can insert jpg, bmp and gif. If you need to insert other types of pictures just import them as on the example.

If any questions feel free to contact me.

No comments:

Post a Comment