Access the Windows Clipboard

The clipboard is a set of functions and messages that can be used to transfer data between different windows applications. This is possible since all applications have access to it. We can manipu

ITPro Today

May 9, 2004

3 Min Read
ITPro Today logo in a gray background | ITPro Today

The clipboard is a set of functions and messages that can be used to transfer data between different windows applications. This is possible since all applications have access to it. We can manipulate data from the Windows clipboard through the .NET framework.

The code sample below shows how to access an image from the clipboard and save it to a file.

We can use the System.Windows.Forms.Clipboard class, which provides methods to save data to the system clipboard and retrieve data from it.

All methods of this class are static, so we don’t have to create an instance of it and everybody has serialized access to it.

The GetDataObject() method is the method that gets all the data from the clipboard.
The other method GetDataPresent()

Given below is the code.

Code of article

'The IDataInterface is used to get the Data

'from the clipboard since it can be in any format

Dim oData As IDataObject

Dim oImg As Image

 

'The GetDataObject() method is used to get the data

'from the clipboard

If Not IsNothing(Clipboard.GetDataObject) Then

    oData = Clipboard.GetDataObject()

    'The GetDataPresent() method is used to check for

    'a particular format of Data

    If oData.GetDataPresent(DataFormats.Bitmap) Then

        oImg = oData.GetData(DataFormats.Bitmap, True)

        oImg.Save("myImage.gif",System.Drawing.Imaging.ImageFormat.Gif)

    Else

        MessageBox.Show("Clipboard data Not an Image")

    End If

Else

    MessageBox.Show("Clipboard has No Data")

End If

 

 

Hope this piece of code helps. Happy Coding !!

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like