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

Clipboard handling with .NET

, 27 Apr 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
An introduction to using the clipboard

The Clipboard

The clipboard is where your data is stored when you copy or cut some text or an image or a file or a folder. The clipboard is common across processes, which means we can use the clipboard as a mechanism to transfer data between two processes. Win32 gave us a Clipboard API with functions like OpenClipboard and GetClipboardData which most of you might be familiar with. In .NET Microsoft has encapsulated the functionality of most of these functions into some classes. The most prominent of these classes is a class which has been named aptly as Clipboard. It's a very simple class with just two functions. In this article, we'll see how to copy/paste text, images and also how to maintain clipboard data in multiple formats.

I'd like to thank James Johnson for his amazing help while I was fiddling with the clipboard. I didn't have a reference book to help me, but that didn't matter much as James more than made up for it. Poor Mike Dunn had to suffer some .NET talk when he walked into Bob's HungOut just as I was discussing an issue with James. Anyway let's get on with things now.

Copying text into the clipboard

The Clipboard class has a SetDataObject function which is used to insert clipboard data. It takes an Object as argument in the single argument overload. In the other overload it takes both an Object and a bool variable, which we can set to true if we want the clipboard to persist even after our application has exited. In our example program we'll use the second overload as that seems to be the more common situation, where we would want other programs to access what we have copied onto the clipboard.


Retrieving text from the clipboard

The Clipboard class also has a GetDataObject function which returns an IDataObject. The IDataObject interface has a function called GetDataPresent which we use to determine if the IDataObject has data of a specific format.  We can use the static fields of the DataFormats class to verify that the format we expect is indeed available in the IDataObject. If GetDataPresent returns true, we can then use the GetData function to retrieve the data format that we are expecting.

    textBox1.Text = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();
    textBox1.Text  = "The clipboad does not contain any text";

Observe how I have copied some text from Notepad and pasted it into our sample program. You can also copy text from our program and paste it into Notepad. And I hope you like my XP theme and colors as well.

Copying/Retrieving images from the Clipboard

This is exactly the same as in copying and pasting text except that we use the DataFormats.Bitmap format instead of DataFormats.Text

    pictureBox1.Image = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);

I have copied from Paint Brush into our sample program. You can then copy something else using Paint Brush, come back to our program, copy the image and paste back into Paint Brush. The image has stretched a bit because I have set the SizeMode  property of the Picture control to StretchImage.

Maintain multiple formats in the Clipboard

There are situations where you are not sure what kind of data the target application of a copy/paste cycle is expecting in the clipboard. In such cases we can maintain multiple data formats in the clipboard, so that the target application can retrieve the data format it's expecting. In our sample, we'll copy both the text in the edit box as well as the image in the picture control into the clipboard. Now you can open Notepad and paste into it the text as well as open Paint Brush and paste into it the bitmap. I have provided a [Paste Both] button for ease of demonstration. Just run a second instance of the sample program and we can paste both the text and the image into the respective controls.

We make use of the DataObject class which implements the IDataObject interface. We call the SetData function twice, each time passing in a different format data. We use the following override of the function :-

public virtual void SetData(
   string format, //This indicates what type of data this is going to be
   bool autoConvert, //Set this true to allow automatic data format conversions
   object data //The actual data [which is in the format specified above]

The code is very straightforward. We create a DataObject object, use SetData twice, once with the text and then with the image, then we add this DataObject to the clipboard.

DataObject m_data = new DataObject();

Now you can paste both into Notepad as well as into any Graphics application that allows you to paste a Bitmap into it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nish Nishant

United States United States
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff -
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Comments and Discussions

QuestionGetting null PinmemberStephin Francis22-Dec-14 2:46 
QuestionHow change Color Background of Clipboard? Pinmemberalias13679214-Mar-13 1:12 
Questionhave copied tabs seprated string, now how to put each string in a different textbox? PinmemberMember 89732142-Jan-13 13:30 
GeneralMy vote of 4 PinmemberCodeMan482531-Jul-12 22:55 
GeneralMy vote of 5 Pinmemberdavod_ir7-Jan-12 2:23 
QuestionCan it be applied to web form? Pinmemberping12343-Feb-10 20:34 
QuestionHow to copy Bitmap with Transparency (Alpha Channel) into the clipboard ? PinmemberSLA8028-Dec-09 19:52 
AnswerRe: How to copy Bitmap with Transparency (Alpha Channel) into the clipboard ? Pinmembervanhalsen18-Jan-10 8:07 
AnswerRe: How to copy Bitmap with Transparency (Alpha Channel) into the clipboard ? PinmemberAndy Missico9-May-10 11:39 
QuestionHow to Handle Clipboard Paste Pinmembereg_Anubhava28-Aug-09 0:14 
QuestionWon't paste the image into Open Office Writer? Pinmemberldcr11-Aug-09 15:05 
GeneralClipboard for Web Base Application Pinmemberamyvoon15-Apr-09 22:56 
Questionhow to get the multiple file names from the copied folder from the clipboard C# 2005 PinmemberMember 447950427-Jan-09 22:46 
GeneralClipboard.SetText vs. Clipboard.SetDataObject [modified] Pinmembersmhiker10-Nov-08 11:49 
GeneralNice Article Pinmemberashu_himt16-Apr-08 4:54 
GeneralDoesn't work PinmemberDeebadeebz11-Apr-08 10:36 
GeneralCopying images to clipboard using Pinmembertmgovind8-Apr-08 2:52 
GeneralullReferenceException was unhandled <------PROBLEM PinmemberLOLKICA2-Jul-07 10:50 
GeneralThanks PinmemberTodd Smith2-May-07 9:35 
Generalcopy image from clipboard and insert into word doc Pinmemberminad_78615-Mar-07 5:55 
GeneralInteraction c#/c++ Pinmemberruedig5-Oct-06 6:06 
QuestionSTAThread [modified] Pinmemberwurakeem24-Sep-06 5:27 
AnswerRe: STAThread Pinmemberwurakeem24-Sep-06 12:31 
GeneralRe: STAThread PinmemberMember 276523528-May-08 1:12 
GeneralSome Issues PinmemberYaron Sh4-Sep-06 0:01 
The common task is to copy selected text. The form's active control might be label, list item or other and not all of them have the SelectedText property.
Normal copy should handle all these situations.
This article doesn't provide the common functionality basics.
All I can think of right now is some factory that can identify the control type and then do copy for it's content. (Selected text for text based controls, images for image based controls etc.).

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 | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 28 Apr 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid