Copy, Cut, and Paste Parts of An Image To The Clipboard in C# - C# HelperC# Helper
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.
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
Rectangle dest_rect =
GraphicsUnit.Pixel);
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.
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.
CopyToClipboard(SelectedRect);
gr.FillRectangle(br, SelectedRect);
picImage.Image = SelectedImage;
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.
if (!Clipboard.ContainsImage()) return;
int cx = SelectedRect.X +
(SelectedRect.Width - clipboard_image.Width) / 2;
int cy = SelectedRect.Y +
(SelectedRect.Height - clipboard_image.Height) / 2;
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);
gr.DrawImage(clipboard_image, dest_rect);
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.
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
0, 0,
clipboard_image.Width,
clipboard_image.Height);
gr.DrawImage(clipboard_image, SelectedRect,
src_rect, GraphicsUnit.Pixel);
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.
sanal says:
October 30, 2014 at 8:37 am
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
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
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:
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.
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?
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