Click here to Skip to main content
13,250,182 members (57,159 online)
Click here to Skip to main content
Add your own
alternative version


118 bookmarked
Posted 27 Apr 2002

Clipboard handling with .NET

, 27 Apr 2002
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

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
. 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 Nishant is the Principal Software Architect/Consultant for Ganymede Software Solutions LLC, and is based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish.

Company Website :

You may also be interested in...


Comments and Discussions

QuestionHow to Handle Clipboard Paste Pin
eg_Anubhava28-Aug-09 0:14
membereg_Anubhava28-Aug-09 0:14 
QuestionWon't paste the image into Open Office Writer? Pin
ldcr11-Aug-09 15:05
memberldcr11-Aug-09 15:05 
GeneralClipboard for Web Base Application Pin
amyvoon15-Apr-09 22:56
memberamyvoon15-Apr-09 22:56 
Questionhow to get the multiple file names from the copied folder from the clipboard C# 2005 Pin
Member 447950427-Jan-09 22:46
memberMember 447950427-Jan-09 22:46 
GeneralClipboard.SetText vs. Clipboard.SetDataObject [modified] Pin
smhiker10-Nov-08 11:49
membersmhiker10-Nov-08 11:49 
GeneralNice Article Pin
ashu_himt16-Apr-08 4:54
memberashu_himt16-Apr-08 4:54 
GeneralDoesn't work Pin
Deebadeebz11-Apr-08 10:36
memberDeebadeebz11-Apr-08 10:36 
GeneralCopying images to clipboard using Pin
tmgovind8-Apr-08 2:52
membertmgovind8-Apr-08 2:52 
GeneralullReferenceException was unhandled <------PROBLEM Pin
LOLKICA2-Jul-07 10:50
memberLOLKICA2-Jul-07 10:50 
GeneralThanks Pin
Todd Smith2-May-07 9:35
memberTodd Smith2-May-07 9:35 
Generalcopy image from clipboard and insert into word doc Pin
minad_78615-Mar-07 5:55
memberminad_78615-Mar-07 5:55 
GeneralInteraction c#/c++ Pin
ruedig5-Oct-06 6:06
memberruedig5-Oct-06 6:06 
QuestionSTAThread [modified] Pin
wurakeem24-Sep-06 5:27
memberwurakeem24-Sep-06 5:27 
AnswerRe: STAThread Pin
wurakeem24-Sep-06 12:31
memberwurakeem24-Sep-06 12:31 
GeneralRe: STAThread Pin
Member 276523528-May-08 1:12
memberMember 276523528-May-08 1:12 
GeneralSome Issues Pin
Yaron Sh4-Sep-06 0:01
memberYaron Sh4-Sep-06 0:01 
GeneralClipboard and Metafiles Pin
raastad5-Apr-06 0:11
memberraastad5-Apr-06 0:11 
GeneralCopy files to clipboard Pin
Maxim Kogan26-Sep-05 4:13
sussMaxim Kogan26-Sep-05 4:13 
GeneralRe: Copy files to clipboard Pin
Vertexwahn19-Dec-05 23:57
memberVertexwahn19-Dec-05 23:57 
GeneralWere the files CUT or COPIED.... Pin
AndrewVos17-Aug-05 4:57
memberAndrewVos17-Aug-05 4:57 
GeneralRe: Were the files CUT or COPIED.... Pin
alexstnky30-Aug-05 22:45
memberalexstnky30-Aug-05 22:45 
GeneralRe: Were the files CUT or COPIED.... Pin
AJafer23-Jul-07 1:50
memberAJafer23-Jul-07 1:50 
GeneralOne more doubt about Clipboard copy Pin
jibesh17-Jun-05 22:43
memberjibesh17-Jun-05 22:43 
GeneralDoesn't Work Pin
dfhgesart8-Jun-05 8:29
sussdfhgesart8-Jun-05 8:29 
GeneralRe: Doesn't Work Pin
AJafer23-Jul-07 1:49
memberAJafer23-Jul-07 1:49 
Generalwell done Pin
Ghazi Al Wadi14-May-05 20:31
memberGhazi Al Wadi14-May-05 20:31 
QuestionHow to put metafile on clipboard? Pin
hain14-May-05 15:19
memberhain14-May-05 15:19 
AnswerRe: How to put metafile on clipboard? Pin
Liu Junfeng6-Jun-05 6:59
memberLiu Junfeng6-Jun-05 6:59 
GeneralRetrieve all objects in clipboard Pin
alon ronen22-Feb-05 23:16
memberalon ronen22-Feb-05 23:16 
QuestionCopy selected text? Pin
Johnpl9-Dec-04 10:51
memberJohnpl9-Dec-04 10:51 
AnswerRe: Copy selected text? Pin
Stefan Prodan12-Mar-05 2:28
memberStefan Prodan12-Mar-05 2:28 
GeneralText Widget and Image Widget Pin
Anonymous9-Nov-04 8:35
sussAnonymous9-Nov-04 8:35 
GeneralRetreiving a CF_BITMAP format from Clipboard Pin
Anonymous11-Apr-04 11:22
sussAnonymous11-Apr-04 11:22 
GeneralRe: Retreiving a CF_BITMAP format from Clipboard Pin
sabrown10017-Oct-07 13:13
membersabrown10017-Oct-07 13:13 
Generalgdi+ and clipboard Pin
cowen1-Aug-03 3:45
membercowen1-Aug-03 3:45 
GeneralCut and Paste Files from Explorer Pin
Alex Cherkasov24-Jul-03 7:01
memberAlex Cherkasov24-Jul-03 7:01 
Generala Good Job!!! Pin
Vitaliy Vorontsov19-Sep-02 0:37
memberVitaliy Vorontsov19-Sep-02 0:37 
GeneralRe: a Good Job!!! Pin
Nishant S19-Sep-02 0:43
editorNishant S19-Sep-02 0:43 
QuestionHow do i copy from another form? Pin
Dario Argentino7-Aug-02 18:35
sussDario Argentino7-Aug-02 18:35 
GeneralInstructive article! Pin
Martin "Lucanus cervus"30-Apr-02 23:53
memberMartin "Lucanus cervus"30-Apr-02 23:53 
GeneralRe: Instructive article! Pin
Nish - Native CPian3-Jun-02 23:51
memberNish - Native CPian3-Jun-02 23:51 
GeneralNot strictly about this article, but Drag and Drop Pin
Paul Ingles29-Apr-02 6:07
memberPaul Ingles29-Apr-02 6:07 
GeneralRe: Not strictly about this article, but Drag and Drop Pin
Paul Ingles14-May-02 23:11
memberPaul Ingles14-May-02 23:11 
GeneralNice work Nish! Pin
Ernest Laurentin29-Apr-02 5:38
memberErnest Laurentin29-Apr-02 5:38 
GeneralRe: Nice work Nish! Pin
Nish [BusterBoy]29-Apr-02 5:51
memberNish [BusterBoy]29-Apr-02 5:51 
GeneralNice work Pin
Michael P Butler28-Apr-02 5:02
memberMichael P Butler28-Apr-02 5:02 
GeneralRe: Nice work Pin
David Wulff28-Apr-02 6:25
memberDavid Wulff28-Apr-02 6:25 
GeneralRe: Nice work Pin
Michael P Butler28-Apr-02 6:29
memberMichael P Butler28-Apr-02 6:29 
GeneralRe: Nice work Pin
David Wulff28-Apr-02 6:36
memberDavid Wulff28-Apr-02 6:36 
GeneralRe: Nice work Pin
Nish [BusterBoy]30-Apr-02 16:04
memberNish [BusterBoy]30-Apr-02 16:04 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 28 Apr 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid