See what kinds of data are available in the clipboard and display them in C#


This example shows how you can see what kinds of data are available in the clipboard. The program’s form contains four controls that can display samples of data. They include a PictureBox, TextBox, RichTextBox, and WebBrowser.

Before the WebBrowser can display HTML loaded from code, it needs a Document object. The program gives it one by navigating to the blank page when the program starts.

private void Form1_Load(object sender, EventArgs e)

After navigation is complete, the code can use the control’s Document property to display HTML.

When you click the List Formats button, the following code displays the available clipboard formats and shows samples of the kinds of data it can understand.

// List the available formats.
private void btnListFormats_Click(object sender, EventArgs e)
    IDataObject data_object = Clipboard.GetDataObject();
    foreach (string format in data_object.GetFormats())

This event handler gets the clipboard’s data object and clears the program’s ListBox. It then loops through the data formats returned by the data object’s GetFormats method and displays them in the ListBox. It finishes by calling the following DisplayData method to display whatever data it can.

// Display data if possible.
private void DisplayData()
    txtSample.Visible = false;
    rchSample.Visible = false;
    picSample.Visible = false;
    wbrSample.Visible = false;

    // Image.
    if (Clipboard.ContainsImage())
        picSample.Image = Clipboard.GetImage();
        picSample.Visible = true;

    // Text.
    if (Clipboard.ContainsText(TextDataFormat.UnicodeText))
        txtSample.Text = Clipboard.GetText(
        txtSample.Visible = true;

    // HTML.
    if (Clipboard.ContainsText(TextDataFormat.Html))
        HtmlDocument doc = wbrSample.Document;
        doc.Body.InnerHtml = Clipboard.GetText(
        wbrSample.Visible = true;

    // Rich Text.
    if (Clipboard.ContainsText(TextDataFormat.Rtf))
        rchSample.Rtf = Clipboard.GetText(
        rchSample.Visible = true;

This code hides the program’s four display controls (so they don’t clutter things up if there’s no data for them to display). It then uses the Clipboard object’s ContainsXxx methods to see what kinds of data are available on the clipboard. If the clipboard contains image, Unicode, HTML, or RTF data, the program displays it.

Notice that the code uses the clipboard’s GetText method to get text, HTML, and RTF data. It passes GetText the kind of text it wants.

Take a look at the picture of the example program. Here I opened a Word file (included in the download) that contains an image and text in various fonts and colors. Note that the program doesn’t say there is image data available in the clipboard. The selected text contains an image, but there is no image available by itself.

Also notice that the program shows that there are just over a dozen formats available, all provided by Word to the clipboard.

Download Example   Follow me on Twitter   RSS feed

This entry was posted in clipboard and tagged , , , , , , , , . Bookmark the permalink.

2 Responses to See what kinds of data are available in the clipboard and display them in C#

  1. Adrian says:

    I found that System.Windows.Forms.Clipboard.ContainsText() occasionally throws System.Runtime.InteropServices.ExternalException
    I could not determine the exact cause but catching the exception and retrying after a short pause worked fine.

  2. Rod Stephens says:

    I guess I’ve been “lucky” and haven’t seen this, but after Googling around a bit it seems that this can happen if another thread is accessing the clipboard at the same time. It sounds like the workaround is what you’re doing: catch the exception, wait a little bit, and try again.

    Thanks for pointing this out!

Comments are closed.