65.9K
CodeProject is changing. Read more.
Home

Using the clipboard to transfer data to and from your applications

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (11 votes)

Jan 10, 2001

viewsIcon

100334

downloadIcon

1879

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!");