C # WinForm: paste the image to PictureBox and copy the image in PictureBox.

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.