[WPF] Prevent the user from pasting an image in a RichTextBox

WPF’s RichTextBox control is quite powerful, and very handy if you need to accept rich text input. However, one of its features can become an issue: the user can paste an image. Depending on what you intend to do with the text entered by the user, you might not want that.

When I googled for a way to prevent that, the only solutions I found suggested to intercept the Ctrl-V keystroke, and swallow the event if the clipboard contains an image. There are several issues with this approach:

  • it doesn’t prevent the user from pasting using the context menu
  • it won’t work if the command’s shortcut has been changed
  • it doesn’t prevent the user from inserting an image using drag and drop

Since I wasn’t satisfied with this solution, I used the .NET Framework Reference Source website to look for a way to intercept the paste operation itself. I followed the code from the ApplicationCommands.Paste property, and eventually found the DataObject.Pasting attached event. It’s not a place where I had thought to look, but when you think about it, it actually makes sense. This event can be used to intercept a paste or drag and drop operation, and lets the hander do a few things:

  • cancel the operation completely
  • change which data format will be pasted from the clipboard
  • replace the DataObject used in the paste operation

In my case, I just wanted to prevent an image from being pasted or drag and dropped, so I just cancelled the operation when the FormatToApply was "Bitmap", as shown below.


<RichTextBox DataObject.Pasting="RichTextBox1_Pasting" ... />


private void RichTextBox1_Pasting(object sender, DataObjectPastingEventArgs e)
    if (e.FormatToApply == "Bitmap")

Of course, it’s also possible to handle this in a smarter way. For instance, if the DataObject contains several formats, we could create a new DataObject with only the acceptable formats. This way the user is still able to paste something, if not the image.

6 thoughts on “[WPF] Prevent the user from pasting an image in a RichTextBox”

    1. Hi Franz,

      Good point. The handler probably needs to be smarter than that, and check the actual contents if the format is “Rich Text Format” or “XamlPackage”.

  1. The following allows you to handle any of the commands. This also prevents images from being pasted so it works quite nicely.

    CommandManager.AddPreviewExecutedHandler(this, OnPreviewExecuted);

    protected virtual void OnPreviewExecuted(object sender, ExecutedRoutedEventArgs e)
    if (e.Command.EqualsAny(ApplicationCommands.Copy, ApplicationCommands.Cut, ApplicationCommands.Paste))
    e.Handled = true;

    Assuming you define a new class that inherits RichTextBox; however, you can always turn this into an AttachedProperty 🙂

    1. Hi James,

      Yes, you can do that too, but it prevents the user from pasting anything, and it’s not what I was trying to achieve. I only wanted to prevent images from being pasted 😉

      1. Couldn’t you just check if the clipboard contains any contents? For instance, you can do:

        if (e.Command == ApplicationCommands.Paste && Clipboard.ContainsImage())
        e.Handled = true;

        The Clipboard class has additional similar methods for checking other data types, like files, text, audio, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *