Click here to Skip to main content
11,789,512 members (47,638 online)
Click here to Skip to main content

Using Clipboard C# 4.0 (Wrapper inside)

, 2 Oct 2012 CPOL 12.6K 777 16
Rate this:
Please Sign up or sign in to vote.
Library that manages all objects entering and leaving the Windows clipboard.


How many times have you wanted a library that manages all objects entering and leaving the windows clipboard? Maybe without having to handle the various types of objects ..

Here it is!

Nothing could be simpler. This library will handle everything for you through a series of delegates to communicate the appearance of new notes and a thread that has the task of effectively monitor system clipboard.

This library track:

  • Text and html; 
  • Images;
  • Files;

The library needs, for now, a Windows Forms object for the proper management of the delegates. Future versions will replace this part with an interface that will manage the delegates. It will be for those who will implement this interface to manage the Invoke in the form (or XAML view) correctly.

Using the code

// Use these methods to interact directly with the clipboard without using the manager
// These methods are a common wrapper namespace "System.Windows.Forms.Clipboard"
string[] files = ClipboardManager.GetClipboardFiles();
Image image = ClipboardManager.GetClipboardImage();
string text = ClipboardManager.GetClipboardText();
ClipboardManager.SetClipboardFiles(new string[] { "file1", "file2" });
ClipboardManager.SetClipboardImage(new Bitmap("path"));

You can use the library only in a static way. This way you can read and write files, images and text to the clipboard. Please note that in case the clipboard was empty or tried, for example, to read a text in the clipboard when there is a file, static methods will return always null. The use of "Clipboard Manager" as static class is not recommended in cases where there is no certainty of what's in the clipboard.

// Use the "ClipboardManager" to manage in a more comprehensive the clipboard
// I assume that "this" is a Form
ClipboardManager manager = new ClipboardManager(this);
// Use "All" to handle all kinds of objects from the clipboard
// otherwise use "Files", "Image" or "Text"
manager.Type = ClipboardManager.CheckType.All;
// Use events to manage the objects in the clipboard
manager.OnNewFilesFound += (sender, eventArg) => 
    foreach (String item in eventArg)
        Console.WriteLine("New file found in clipboard : {0}", item);
manager.OnNewImageFound += (sender, eventArg) =>
    Console.WriteLine("New image found in clipboard -> Width: {0} , Height: {1}", 
                      eventArg.Width, eventArg.Height);
manager.OnNewTextFound += (sender, eventArg) =>
    Console.WriteLine("New text found in clipboard : {0}", eventArg);
// Use the method "StartChecking" to start capturing objects in the clipboard
// Close the capturing

Using the class "Clipboard Manager" as an instance you have the possibility of having, by event, reporting that a new object is entered into the clipboard. This class is very convenient because it allows the user to manage the objects in the clipboard in near real time (the manager operates through a thread and so there may be about a one second delay).


 02/10/2012: Version (Resolved image comparison bug + adding test project) 

28/09/2012: Version 


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


About the Author

D.Net Solutions
Software Developer (Senior) D.Net Solution
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

SuggestionA few things... Pin
Andrew Rissing28-Sep-12 5:50
memberAndrew Rissing28-Sep-12 5:50 
GeneralRe: A few things... Pin
D.Net Solutions1-Oct-12 2:59
memberD.Net Solutions1-Oct-12 2:59 
GeneralRe: A few things... Pin
Andrew Rissing1-Oct-12 3:48
memberAndrew Rissing1-Oct-12 3:48 
CheckClipBoard() is a thread to notify interested parties of a new image/string/etc. that is added to the clipboard. Inside your current implementation, you've compared the returned image with LastImageFound. While the content of the two images is exactly the same, the comparison made would only be checking to see that the reference is the same and, thus, would always return false. Because of this, the OnNewImageFound method would be fired every second until the code is stopped.

This behavior works the same for your CheckType.Files version as well.
GeneralRe: A few things... Pin
D.Net Solutions2-Oct-12 0:33
memberD.Net Solutions2-Oct-12 0:33 

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
Web02 | 2.8.1509028.1 | Last Updated 2 Oct 2012
Article Copyright 2012 by D.Net Solutions
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid