Click here to Skip to main content
Click here to Skip to main content

Using the clipboard to transfer data to and from your applications

, 1 Feb 2001
Rate this:
Please Sign up or sign in to vote.
An article describing ways to use the clipboard to transfer data to and from applications on the .NET Platform using C#
  • Download demo project - 3 Kb
  • Download source - 1 Kb

    The .NET platform has simplified a lot of things in Beta1. Simplifications Range from window controls to Enterprise web services. In this tutorial we will go through some steps for teaching you how to use the clipboard to transfer data to and from your .NET applications or any other application.

    To transfer clipboard data in .NET we need to use three classes, Clipboard, DataFormats and IDataObject. These classes reside in the System.WinForms namespace.

    Lets start by creating a simple application to check what type of data the clipboard contains. Look at the sample below:

    Checking Clipboard Data Contents Type

    using System;
    using System.WinForms;
    
    public class MyClipboardApp
    {
        public static void Main(string[] args)
        {
            IDataObject d = Clipboard.GetDataObject();
    
            if(d.GetDataPresent(DataFormats.Bitmap)) {
                Console.WriteLine("Bitmap data is Contained in the clipboard.");
            } else if(d.GetDataPresent(DataFormats.Text)) {
                Console.WriteLine("Text data is Contained in the clipboard.");
            } else {
                Console.WriteLine("Some other format of data is Contained in the clipboard.");
            }
        }    
    }

    Lines 1 thru 7 should already be somewhat familiar to you. In Line 8 we assign an Object called d with an IDataObject interface reference that encapsulates the clipboard data. We then check the data contained in the clipboard using the GetDataPresent() method of the IDataObject interface. The parameter that we pass to GetDataPresent() is the type of data we are checking for, these values are predefined in the DataFormats Enumeration. This method returns true if the data encapsulated in this IDataObject instance can be converted to the specified format else the method returns false.

    Once we have the data what can we do with? Well, it is really up to you what you do with the data. But we will go through another example to show you how to deal with the data once you have it.

    IDataObject has a method GetData(). This method returns an Object type associated with the format of the data in the clipboard. The following sample retrieves bitmap data from the clipboard that has been copied from some other application, like Microsoft paint for instance, and saves the data as a bitmap image.

    Retrieving Clipboard Data Contents And Saving

    using System;
    using System.Drawing;
    using System.WinForms;
    
    public class MyClipboardApp
    {
        public static void Main(string[] args)
        {
            IDataObject d = Clipboard.GetDataObject();
    
            if(d.GetDataPresent(DataFormats.Bitmap)) {
                Bitmap b = (Bitmap)d.GetData(DataFormats.Bitmap);
                b.Save(@"c:\mybitmap.bmp");
                Console.WriteLine("Bitmap saved to disk!!");
            } else {
                Console.WriteLine("No bitmap information was contained in the clipboard.");
            }
        }    
    
    }

    Before testing this sample, doodle about in paint, select an area then copy it. Then run the program and look at the output. It is very important to check the data format in the clipboard before you use it. This is because GetData() returns null if the two formats are incompatible and will stop normal program execution.

    It's also pretty easy to put information into the clipboard programmatically, using only a few lines of code.

    Copying Data To Clipboard

    IDataObject d = Clipboard.GetDataObject();
    d.SetData(DataFormats.Bitmap, new Bitmap(@"c:\myimage.bmp"));

    Or adding text is also easy to do.

    IDataObject d = Clipboard.GetDataObject();
    d.SetData(DataFormats.Text, "Hello World!");
  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Jon Boyce

    United Kingdom United Kingdom
    No Biography provided

    Comments and Discussions

     
    QuestionHow to check type of clipboard data Pinmemberasawant30-Dec-09 20:19 
    Generalbuild a dynamic array PinsussAnonymous10-Apr-05 0:30 
    GeneralGreat Code!............ PinmemberBorey14-Aug-04 21:13 
    Questionwhat would be really neat... Pinmembermliss20-Jan-04 7:41 
    QuestionWHAT ABOUT TO A FILE Pinmemberjphuphilly1-Jul-03 9:46 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Mobile
    Web03 | 2.8.140709.1 | Last Updated 2 Feb 2001
    Article Copyright 2001 by Jon Boyce
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid