block images in richtextbox

quioske

Active member
Joined
Feb 25, 2008
Messages
27
Programming Experience
1-3
Hi,

i have a richtextbox to which i want only the plain text not the images

i want the images to be blocked
i have tried it but when the user copies a single image in clipboard. i am able to block that image.the code is

If Clipboard.ContainsImage = True Then
Clipboard.Clear()
End If

but if user copies image and text both then it s problem
anyone knows it
 
Firstly quioske, never change the clipboard object unless the user have explicitly ordered your application to do so!

Since RichTextBox doesn't have a Images or Objects collection you have to go for the RTF formatting codes. All data of RichTextBox is stored as plain text with special formatting codes, this is exposed by the control through its RTF property. Learning this code language is essential if you want to read or change it, learning resources are easily available throughout the web, see for example this overview. RichTextBox uses more simplified rtf codes than several full-feature editors like MS Word etc, so it is usually beneficial to load data into a RTB before manipulating it, this will remove much redundant data.

Making a long story short, I found that it is necessary to search for rtf groups that start with either "pict" or "object" command. Knowing that groups may be nested you can't just find the first end-group char from there, you have to parse the string char by char while keeping count of grouping to find the end of those groups. Now you have enough information to remove that part of the string. Rtf may contain multiple picture/object groups so you have to do this until all are removed. Here is a sample function that return rtf string after removing those groups:
VB.NET:
Private Function removeRtfObjects(ByVal rtf As String) As String
    'removing {\pict or {\object groups
    Dim pattern As String = "\{\\pict|\{\\object"
    Dim m As Match = Regex.Match(rtf, pattern)
    Do While m.Success
        Dim count As Integer = 1
        For i As Integer = m.Index + 2 To rtf.Length
            If rtf(i) = "{"c Then 'start group
                count += 1
            ElseIf rtf(i) = "}"c Then 'end group
                count -= 1
            End If
            If count = 0 Then 'found end of pict/object group
                rtf = rtf.Remove(m.Index, i - m.Index + 1)
                Exit For
            End If
        Next
        m = Regex.Match(rtf, pattern) 'go again
    Loop
    Return rtf
End Function
When should this be done? You have already mention Paste, there is also Insert, these can be trapped with the KeyDown event where you get the clipboard info and handle it accordingly. Setting e.Handled=True when you have handled the operation yourself signals that the control should not do any default processing for this key combination. This is also how you block pasting images without destroying the users clipboard. Example:
VB.NET:
Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles RichTextBox1.KeyDown
    'aware of Paste or Insert
    If e.Control AndAlso e.KeyCode = Keys.V _
    OrElse e.Shift AndAlso e.KeyCode = Keys.I Then
        If Clipboard.ContainsImage OrElse Clipboard.ContainsFileDropList Then
            'some images are transferred as filedrops
            e.Handled = True 'stops here
        ElseIf Clipboard.ContainsData(DataFormats.Rtf) Then
            Dim rtbox As New RichTextBox 'use a temp box to validate/simplify
            rtbox.Rtf = Clipboard.GetData(DataFormats.Rtf)
            Me.RichTextBox1.SelectedRtf = Me.removeRtfObjects(rtbox.Rtf)
            rtbox.Dispose()
            e.Handled = True
        End If
    End If
End Sub
There may also be such objects in initial loaded documents, in that case you just do the same, load the data secretly and manipulate it, before displaying it in UI RichTextBox.
 
thanks john ,

its a great peice of work

firstly ,what i am doing is copying the contents of richtextbox in a string
then checking for the ctrl+v and if richtextbox contains any image then i am replacing the richtextbox contents with rs(string variable)

but yours solution is good one and worked for me,

thanks a lot

VB.NET:
    Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown

dim rs as string =richtextbox.rtf
end sub

VB.NET:
Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
If (keyAsc = 22) Then
        If InStr(RichTextBox1.Rtf, "{\pict\wmetafile8") Then
      RichTextBox1.Clear()
      RichTextBox1.Rtf = rs
  End If
  End If
end sub
 
Last edited:
Back
Top