Let the user copy, cut, and paste parts of an image selected with a rubber band box in C#

The first step is letting the user select an area with a rubber band box. For a description of how that 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 CopyToClipboard function 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(m_OriginalImage, dest_rect, src_rect, GraphicsUnit.Pixel);
}

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

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

When the user selects the Edit menu's Copy command, the code simply calls CopyToClipboard and beeps so you know if did something.

When the user selects the Edit menu's Cut command, the code also calls CopyToClipboard. It then clears the selected area.

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

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

// Display the result.
m_SelectedImage = new Bitmap(m_OriginalImage);
picImage.Image = m_SelectedImage;

// Disable the buttons.
EnableMenuItems();
m_SelectedImage = null;
m_SelectedGraphics = null;
m_MadeSelection = false;

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

This code calls CopyToClipboard. 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 = m_SelectedRect.X + (m_SelectedRect.Width - clipboard_image.Width) / 2;
int cy = m_SelectedRect.Y + (m_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(m_OriginalImage))
{
gr.DrawImage(clipboard_image, dest_rect);
}

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

m_SelectedImage = null;
m_SelectedGraphics = null;
m_MadeSelection = false;
}

The code uses the Clipboard'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 copies the image to that spot and displays 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(m_OriginalImage))
{
gr.DrawImage(clipboard_image, m_SelectedRect,
src_rect, GraphicsUnit.Pixel);
}

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

m_SelectedImage = null;
m_SelectedGraphics = null;
m_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.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.