You are on page 1of 9

28/9/21 19:36 Copy, cut, and paste parts of an image to the clipboard in C# - C# HelperC# Helper

C# Helper
Tips, tricks, and example programs for
C# programmers.

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


Posted on August 14, 2014 by Rod Stephens

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)

csharphelper.com/blog/2014/08/copy-cut-and-paste-parts-of-an-image-to-the-clipboard-in-c/ 1/9
28/9/21 19:36 Copy, cut, and paste parts of an image to the clipboard in C# - C# HelperC# Helper

// 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();

csharphelper.com/blog/2014/08/copy-cut-and-paste-parts-of-an-image-to-the-clipboard-in-c/ 2/9
28/9/21 19:36 Copy, cut, and paste parts of an image to the clipboard in C# - C# HelperC# Helper

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;

csharphelper.com/blog/2014/08/copy-cut-and-paste-parts-of-an-image-to-the-clipboard-in-c/ 3/9
28/9/21 19:36 Copy, cut, and paste parts of an image to the clipboard in C# - C# HelperC# Helper

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,

csharphelper.com/blog/2014/08/copy-cut-and-paste-parts-of-an-image-to-the-clipboard-in-c/ 4/9
28/9/21 19:36 Copy, cut, and paste parts of an image to the clipboard in C# - C# HelperC# Helper

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;

csharphelper.com/blog/2014/08/copy-cut-and-paste-parts-of-an-image-to-the-clipboard-in-c/ 5/9
28/9/21 19:36 Copy, cut, and paste parts of an image to the clipboard in C# - C# HelperC# Helper

// 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.

csharphelper.com/blog/2014/08/copy-cut-and-paste-parts-of-an-image-to-the-clipboard-in-c/ 6/9
28/9/21 19:36 Copy, cut, and paste parts of an image to the clipboard in C# - C# HelperC# Helper

   

This entry was posted in clipboard, graphics, image processing and tagged C#, C# programming, clipboard, copy, copy image, cut, cut image, example, example program,
graphics, paste, paste image, rubber band box, select image, Windows Forms programming. Bookmark the permalink.

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

sanal says:
October 30, 2014 at 8:37 am

Dear Rod Stephens,

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

RodStephens says:
October 31, 2014 at 2:32 pm

See this example:

Select parts of a scaled image in a PictureBox with different SizeMode values in C#

csharphelper.com/blog/2014/08/copy-cut-and-paste-parts-of-an-image-to-the-clipboard-in-c/ 7/9
28/9/21 19:36 Copy, cut, and paste parts of an image to the clipboard in C# - C# HelperC# Helper

Di says:
September 13, 2015 at 4:31 am

Thanks so much!

Ramesh says:
April 24, 2016 at 10:44 pm

how to show the pixel of a image ?

RodStephens says:
April 25, 2016 at 8:18 am

It depends on what you mean. My guess is you want to enlarge the image until the pixels are easy to see. See this
post:

Resize an image to view its pixels in C#

sudhakar says:
April 27, 2016 at 2:25 am

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

RodStephens says:
April 27, 2016 at 12:33 pm

csharphelper.com/blog/2014/08/copy-cut-and-paste-parts-of-an-image-to-the-clipboard-in-c/ 8/9
28/9/21 19:36 Copy, cut, and paste parts of an image to the clipboard in C# - C# HelperC# Helper

Sorry but undo is a whole different topic. You need to keep some sort of record of what has changed so you can undo
and redo changes. For this example, you could keep a collection of past images before cuts were made.

For more details, see this example:

Provide undo and redo in C#

amal says:
May 4, 2018 at 3:11 am

thanks for you but how i can draw the rectangle and what the event i can use to draw this?

please answer the question

RodStephens says:
May 4, 2018 at 8:09 am

Download the example to see all of the details. The MouseDown, MouseMove, and MouseUp events let you draw the
rectangle. To draw the rectangle, the program redraws the image and then draws the rectangle on top of it.

C# Helper
Proudly powered by WordPress.

csharphelper.com/blog/2014/08/copy-cut-and-paste-parts-of-an-image-to-the-clipboard-in-c/ 9/9

You might also like