Copy, cut, and paste parts of an image to the clipboard in C#

example

The first step in copying, cutting, or pasting an image to the clipboard is to use a rubber band box to select the part of the image to manipulate. This example lets you use a rubber band box to select the area. For a description of how the rubber band box works, see Use a rubber band box to let the user select an area in a picture in C#.

All that remains is learning how to copy, cut, and paste to the clipboard.

The following CopyToClipboard method copies the selected area to the clipboard.

// Copy the selected area to the clipboard.
private void CopyToClipboard(Rectangle src_rect)
{
    // Make a bitmap for the selected area's image.
    Bitmap bm = new Bitmap(src_rect.Width, src_rect.Height);

    // Copy the selected area into the bitmap.
    using (Graphics gr = Graphics.FromImage(bm))
    {
        Rectangle dest_rect =
            new Rectangle(0, 0, src_rect.Width, src_rect.Height);
        gr.DrawImage(OriginalImage, dest_rect, src_rect,
            GraphicsUnit.Pixel);
    }

    // Copy the selection image to the clipboard.
    Clipboard.SetImage(bm);
}

This code creates a Bitmap that has the same size as the selected area. It then copies the corresponding area from the original image to the Bitmap and uses the Clipboard object’s SetImage method to store the Bitmap on the clipboard.

When you select the Edit menu’s Copy command (or press Ctrl+C), the program executes the following code.

// Copy the selected area to the clipboard.
private void mnuEditCopy_Click(object sender, EventArgs e)
{
    CopyToClipboard(SelectedRect);
    System.Media.SystemSounds.Beep.Play();
}

This code simply calls CopyToClipboard and beeps so you know it did something.

When you select the Edit menu’s Cut command (or press Ctrl+X), the program executes the following code.

// Copy the selected area to the clipboard
// and blank that area.
private void mnuEditCut_Click(object sender, EventArgs e)
{
    // Copy the selection to the clipboard.
    CopyToClipboard(SelectedRect);

    // Blank the selected area in the original image.
    using (Graphics gr = Graphics.FromImage(OriginalImage))
    {
        using (SolidBrush br = new SolidBrush(picImage.BackColor))
        {
            gr.FillRectangle(br, SelectedRect);
        }
    }

    // Display the result.
    SelectedImage = new Bitmap(OriginalImage);
    picImage.Image = SelectedImage;

   // Enable the menu items appropriately.
    EnableMenuItems();
    SelectedImage = null;
    SelectedGraphics = null;
    MadeSelection = false;

    System.Media.SystemSounds.Beep.Play();
}

This code also calls CopyToClipboard. To clear the selected area, it then makes a Graphics object for the original image and fills the selected area with the PictureBox‘s background color.

There are several ways you could define a paste operation. This program provides two paste menu items: one that centers the pasted image on the selected area and one that stretches the pasted image to fill the selected area.

The following code shows how the program centers the pasted image.

// Paste the image on the clipboard, centering it
// on the selected area.
private void mnuEditPasteCentered_Click(object sender, EventArgs e)
{
    // Do nothing if the clipboard doesn't hold an image.
    if (!Clipboard.ContainsImage()) return;

    // Get the clipboard's image.
    Image clipboard_image = Clipboard.GetImage();

    // Figure out where to put it.
    int cx = SelectedRect.X +
        (SelectedRect.Width - clipboard_image.Width) / 2;
    int cy = SelectedRect.Y +
        (SelectedRect.Height - clipboard_image.Height) / 2;
    Rectangle dest_rect = new Rectangle(
        cx, cy,
        clipboard_image.Width,
        clipboard_image.Height);

    // Copy the new image into position.
    using (Graphics gr = Graphics.FromImage(OriginalImage))
    {
        gr.DrawImage(clipboard_image, dest_rect);
    }

    // Display the result.
    picImage.Image = OriginalImage;
    picImage.Refresh();

    SelectedImage = null;
    SelectedGraphics = null;
    MadeSelection = false;
}

The code uses the Clipboard object’s GetImage method to get the image to paste. It then calculates the position where the image must go to be centered over the selected area. It finishes by copying the image to that spot and displaying the result.

The following code shows how the program stretches the pasted image.

// Paste the image on the clipboard, stretching it
// to fit the selected area.
private void mnuEditPasteStretched_Click(object sender, EventArgs e)
{
    // Do nothing if the clipboard doesn't hold an image.
    if (!Clipboard.ContainsImage()) return;

    // Get the clipboard's image.
    Image clipboard_image = Clipboard.GetImage();

    // Get the image's bounding Rectangle.
    Rectangle src_rect = new Rectangle(
        0, 0,
        clipboard_image.Width,
        clipboard_image.Height);

    // Copy the new image into position.
    using (Graphics gr = Graphics.FromImage(OriginalImage))
    {
        gr.DrawImage(clipboard_image, SelectedRect,
            src_rect, GraphicsUnit.Pixel);
    }

    // Display the result.
    picImage.Image = OriginalImage;
    picImage.Refresh();

    SelectedImage = null;
    SelectedGraphics = null;
    MadeSelection = false;
}

The code again uses the Clipboard‘s GetImage method to get the image to paste. It then simply copies the image to the selected area and displays the result.


Download Example   Follow me on Twitter   RSS feed




This entry was posted in clipboard, graphics, image processing and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

7 Responses to Copy, cut, and paste parts of an image to the clipboard in C#

  1. sanal says:

    Dear Rod Stephens,

    Good Application, But its not working when the image size mode is changed in to StretchImage. can you please look it…

  2. Di says:

    Thanks so much!

  3. Ramesh says:

    how to show the pixel of a image ?

  4. sudhakar says:

    How to perform undo when cut operation performed on image in this article ?

Leave a Reply

Your email address will not be published. Required fields are marked *