Retrieving text data from clipboard
Clipboard class has a method, GetDataObject() which
retrieves the data that is currently in the system clipboard. In fact GetDataObject()
returns an object that implements the IDataObject interface, which has 3
dominant methods(GetDataPresent, GetData and GetFormats).
GetDataPresent - Determines if the current data in Clipboard
can be converted to the format we specify.
GetData - Retrieves the data associated with the format we
specify from Windows Clipboard.
Note: DataFormats class has predefined Clipboard data format
names, which can be provided to the above 2 methods as parameter to verify the
data format.
Listing 1 – Retrieve text data from Clipboard
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
string s = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();
}
In the above piece of code, GetData method is used to
retrieve data from the system Clipboard. The format of the data to be retrieved
is provided as parameter. As the system Clipboard is shared by multiple
applications, before retrieving data, we must ensure that the format of current
clipboard data is the same that our application expects. So we make a check for
the data format of the data to be retrieved, with a call to GetDataPresent
method. GetDataPresent method returns a boolean value which determines whether
the current data on the Clipboard can be converted to the data format we
specify. To specify the data format, it is advisable to use the DataFormats
class which contains static fields representing system Clipboard formats. DataFormats.Text
is provided for string data. Alternative to using the DataFormats class is to
provide the name of the format as string.
Setting text data to Clipboard
Clipboard class has a method, SetDataObject(). This method
places the specified data on the Clipboard. This method has 3 interesting
overloaded clones.
SetDataObject(object data) - Places data on the system
Clipboard.
Note: The data placed on Clipboard by this method is non-persistent, ie., the
data is not retained on Clipboard once the application exits.
SetDataObject(object data, bool copy) - Places data on the
system Clipboard and specifies whether the data should remain on the Clipboard
after the application exits.
SetDataObject(object data, bool copy, int RetryTimes, int
RetryDelay) - Attempts to place data on the system Clipboard the specified
number of times and with the specified delay between attempts.
Listing 2 – Setting text data to Clipboard
Clipboard.SetDataObject("C# is the best", true);
In the above piece of code, SetDataObject method sets string
"C# is the best" on the System Clipboard. The text remains on the
clipboard till it is replaced by any application. And the string remains there
even after our application exits, because the second parameter of the method is
set to true, which indicates the data to be persistent.
Retrieving image from Clipboard
This is exactly the same as retrieving text data, except
that the data format given is DataFormats.Bitmap instead of DataFormats.Text
Listing 3 – Retrieving image from Clipboard
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Bitmap))
{
Bitmap bmp = (Bitmap)Clipboard.GetData(DataFormats.Bitmap);
}
GetDataPresent method verifies if an image (bitmap) object
is present on clipboard and GetData method retrieves the image as a bitmap
object.
An alternative for this in .NET 2.0 is
Listing 4 - Retrieving image from
Clipboard(Alternative)
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Bitmap))
{
Bitmap bmp = (Bitmap)Clipboard.GetImage();
}
The GetImage method returns an Image object which can be
directly assigned to Image objects or controls.
Setting Image to Clipboard
This is also similar to setting text to clipboard.
Listing 5 - Setting Image to Clipboard
Clipboard.SetDataObject((Bitmap)lstBoxCpyItms.SelectedItem,
true);
The overloaded versions of SetDataObject has been explained
earlier, in this article.
Till now our discussion has been about the basic usage of
the Clipboard class.
Now we shall get into the ClipboardRing application.