WinForm to cut and paste photos into the system clipboard (with code download)

Time:2020-2-5

scene

Effect

 

 

Click the cut button

 

 

Click Paste button

 

 

Note:

Blog home page:
https://blog.csdn.net/badao_liumang_qizhi

Pay attention to the public address
Domineering procedural ape
Get programming related ebooks, tutorials and free downloads.

Realization

Create a new form with the following layout

 

 

There are mainly two PictureBox controls and four buttons. Below is an Imagelist control, and then add three photos to it.

The first photo of Imagelist is displayed by default when loading a form in the form’s load event

private void Form1_Load(object sender, EventArgs e)
        {
            //The first chapter image in Imagelist is displayed by default when the form is loaded
            pictureBox1.Image = imageList1.Images[0];
            //Previous button is not available when loading
            previousSheet.Enabled = false;
        }

 

Because there are three photos in the Imagelist, set the click event of the next button

private void nextSheet_Click(object sender, EventArgs e)
        {
            //Activate previous button
            previousSheet.Enabled = true;
            //Judge whether it is out of line
            if (i

 

Similarly, set the click event of the previous button

private void previousSheet_Click(object sender, EventArgs e)
        {
            nextSheet.Enabled = true;
            if (i < imageList1.Images.Count)
            {
                i--;
                if (i == 0)
                {
                    previousSheet.Enabled = false;
                }
                if (i >=0)
                {
                    pictureBox1.Image = imageList1.Images[i];
                }
            }
        }

 

Set the click event of the cut button

 

private void cut_Click(object sender, EventArgs e)
        {
            //Places data on the system clipboard and specifies whether to keep the data on the clipboard after exiting the program
            Clipboard.SetDataObject(imageList1.Images [i],false );
            MessageBox. Show ("cut successfully! "Prompt message", messageboxbuttons.ok, messageboxicon. Asterisk);
        }

 

It mainly calls the setdataobject method of clipboard. This method places the data in the system clipboard, and the second parameter specifies whether to keep the data in the clipboard after exiting the program.

Then write the click event of paste button

private void copy_Click(object sender, EventArgs e)
        {
            pictureBox2.Image = Clipboard.GetImage();
            MessageBox. Show ("cut successfully! "Prompt message", messageboxbuttons.ok, messageboxicon. Asterisk);
        }

 

It mainly obtains the photos in the clipboard through the getimage() method of clipboard and sets the data source for the PictureBox on the right.

Code download

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12028591

Recommended Today

On the reference count in PHP string type

Author: Wang Shu Background introduction String type is also a commonly used type. Due to the characteristics of strings, in order to save memory, the same string variables usually share a block of memory space. By reference counting, multiple variables are marked to use this memory. However, after GDB tracking, it is found that not […]