The Code Project View our advertisersClick here for Whole Tomato Software - Home of Visual AssistAdvertise on the CodeProject
Home >> The C# Programming Language >> How To

Using the clipboard to transfer data to and from your applications
By Jon Boyce

An article describing ways to use the clipboard to transfer data to and from applications on the .NET Platform using C# 
 Beginner
 .NET SDK, W2K, .NET, MFC
 Posted 10 Jan 2001
 Updated 2 Feb 2001
Articles by this author
Send to a friend
Printer friendly version
FAQ
What's New
Lounge
Contribute
Message Boards
Broken links? Newsletter
 Email
 Password
Remember me?

MSDN Magazine - Your guide to Microsoft tools, development environments, and technologies for Windows and the Web.
Premium Sponsor
7 users have rated this article. result:
3.29 out of 5.
  • 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!");
    [Top] Sign in to vote for this article:     PoorExcellent  
    MSDN Magazine - Your guide to Microsoft tools, development environments, and technologies for Windows and the Web.
    Premium Sponsor

    View our advertisersClick here for Dundas Consulting - experts in MFC, C++, TCP/IP and ASPAdvertise on the CodeProject
    Hint: For a faster board use IE 4+, choose 'Use DHTML' from the View dropdown and hit 'Set Options'.
     Search (exact phrase)
     View   Per page   Messages since
    New threadTotal Messages for this article: 0 
    Subject 
    Author 
    Date 
    -- No entries --
    Home >> The C# Programming Language >> How To
    Advertise on The Code Project
    Article content copyright Jon Boyce, 2001
    everything else © CodeProject, 1999-2001.

    DevelopersDexDevGuruProgrammers HeavenTek-Tips ForumsTopXMLVisualBuilder.comW3SchoolsXMLPitstopZVONSearch all Partners