Technical points:
Objects placed on the clipboard must be serializable.
The clipboard receives an object that implements the IDataObject interface. You can use this object to "Wrap" a specific data object.
You can call the IDataObject. SetData method multiple times to copy multiple types of objects to the clipboard. Use DataFormat to identify the data in the clipboard.
Before pasting, make sure that the data on the clipboard is in the required format.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Drawing;
Using System. Runtime. Serialization;
Namespace UseClipboard
{
[Serializable]
Class MyPic
{
Public Image pic; // Image
Public string picInfo; // Image Description
}
}
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Namespace UseClipboard
{
Public partial class frmMain: Form
{
Public frmMain ()
{
InitializeComponent ();
}
// Image
Private Image bmp
{
Get
{
Return pictureBox1.Image;
}
Set
{
PictureBox1.Image = value;
}
}
// Image Description
Private string info
{
Get
{
Return txtImageInfo. Text;
}
Set
{
TxtImageInfo. Text = value;
}
}
Private void btnLoadPic_Click (object sender, EventArgs e)
{
ChooseImageFile ();
}
// Select the image www.2cto.com
Private void ChooseImageFile ()
{
If (openFileDialog1.ShowDialog () = DialogResult. OK)
{
String name = openFileDialog1.FileName;
TxtImageInfo. Text = name;
Bmp = new Bitmap (name );
}
}
// Create an object based on user-defined information
Private MyPic CreateMyPicObj ()
{
MyPic obj = new MyPic ();
Obj. pic = bmp;
Obj. picInfo = info;
Return obj;
}
// Copy the object to the clipboard
Private void CopyToClipboard ()
{
// Create a MyPic object
MyPic obj = CreateMyPicObj ();
// Create a data object and load the object of the MyPic type
IDataObject dataobj = new DataObject (obj );
// Other types of data can also be loaded into the data object.
Dataobj. SetData (DataFormats. UnicodeText, info );
Dataobj. SetData (DataFormats. Bitmap, bmp );
// Copy to the clipboard. The second parameter indicates that the clipboard is not cleared when the program exits.
Clipboard. SetDataObject (dataobj, true );
}
Private void btnExit_Click (object sender, EventArgs e)
{
Close ();
}
Private void btnCopyToClipboard_Click (object sender, EventArgs e)
{
CopyToClipboard ();
}
// Obtain data from the clipboard
Private void PasteFromClipboard ()
{
// Do I have the data I need on the clipboard? The format is "project name. Data Format name"
If (Clipboard. ContainsData ("UseClipboard. MyPic") = false) // retrieves data objects based on the specified DataFormat
Return;
// Read data
IDataObject clipobj = Clipboard. GetDataObject ();
// Convert data to the required type
MyPic mypicobj = clipobj. GetData ("UseClipboard. MyPic") as MyPic;
// Extract the required data from the data object
Info = mypicobj. picInfo;
PictureBox1.Image = mypicobj. pic;
If (Clipboard. ContainsData (DataFormats. UnicodeText) = false) // retrieves data objects based on the specified DataFormat
Return;
String str = clipobj. GetData (DataFormats. UnicodeText) as string;
MessageBox. Show (str );
}
Private void btnPasteFromClipboard_Click (object sender, EventArgs e)
{
PasteFromClipboard ();
}
}
}
From the conong age