C # WinForm: paste the image to PictureBox and copy the image in PictureBox.
1. The program design interface is as follows:
Operation Method: press the shortcut key Ctrl + V and click a PictureBox to paste the image.
Press the shortcut key Ctrl + C and click a PictureBox to copy the image.
2. The main functional code is as follows:
Using PastePicture;
ClipboardImage cImg = new ClipboardImage ();
///
/// Monitor Windows messages
/// Reload the WndProc Method for hotkey response
///
///
[SecurityPermission (SecurityAction. LinkDemand, Flags = SecurityPermissionFlag. UnmanagedCode)]
Protected override void WndProc (ref Message m)
{
Const int WM_HOTKEY = 0x0312;
// Press the shortcut key
Switch (m. Msg)
{
Case WM_HOTKEY:
Switch (m. WParam. ToInt32 ())
{
Case 103:
// Copy the image
CImg. CopyPictureToPictureBox (pictureBox, pictureBox1, pictureBox2, pictureBox3 );
Break;
Case 104:
// Paste the image
CImg. PastePictureToPictureBox (pictureBox, pictureBox1, pictureBox2, pictureBox3 );
Break;
}
Break;
}
Base. WndProc (ref m );
}
Private void Form1_Activated (object sender, EventArgs e)
{
// Register the shortcut keys Ctrl + C, Ctrl + V
CImg. RegisterShortcuts (this );
}
Private void Main_FormClosing (object sender, FormClosingEventArgs e)
{
// Release the shortcut keys Ctrl + C, Ctrl + V
CImg. UnregisterShortcuts (this );
}
The following is the code for registering a shortcut key with PastePicture. dll:
///
/// Register the shortcut keys Ctrl + C, Ctrl + V
///
/// Form
Public void RegisterShortcuts (Form form)
{
// Register the Ctrl + C shortcut key
UnsafeNativeMethods. RegisterHotKey (form. Handle, 103, 2, Keys. C );
// Register the Ctrl + V shortcut key
UnsafeNativeMethods. RegisterHotKey (form. Handle, 104, 2, Keys. V );
}
///
/// Release the shortcut keys Ctrl + C, Ctrl + V
///
/// Form
Public void UnregisterShortcuts (Form form)
{
// Release the Ctrl + C shortcut
UnsafeNativeMethods. UnregisterHotKey (form. Handle, 103 );
// Release the Ctrl + V shortcut key
UnsafeNativeMethods. UnregisterHotKey (form. Handle, 104 );
}
The function implementation of this program is very simple. You can try to write the code in PastePicture. dll by yourself. You can download the dll file from the beginning of this article for decompilation and view the code in it.