Click here to Skip to main content
15,860,859 members
Articles / Desktop Programming / MFC
Article

Using the clipboard to transfer data to and from your applications

Rate me:
Please Sign up or sign in to vote.
3.00/5 (11 votes)
1 Feb 2001 99.8K   1.9K   34   8
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


    Written By
    United Kingdom United Kingdom
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionHow to check type of clipboard data Pin
    asawant30-Dec-09 20:19
    asawant30-Dec-09 20:19 
    Generalbuild a dynamic array Pin
    Anonymous10-Apr-05 0:30
    Anonymous10-Apr-05 0:30 
    GeneralGreat Code!............ Pin
    Borey14-Aug-04 21:13
    Borey14-Aug-04 21:13 
    Questionwhat would be really neat... Pin
    M i s t e r L i s t e r20-Jan-04 7:41
    M i s t e r L i s t e r20-Jan-04 7:41 
    QuestionWHAT ABOUT TO A FILE Pin
    jphuphilly1-Jul-03 9:46
    jphuphilly1-Jul-03 9:46 

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

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