Title: Copy an irregular area from a picture to the clipboard in C#
Somtimes it's useful to copy an irregular area from a picture to the clipboard. Before explaining how to do that, let me say right away that MS Paint doesn't understand transparency. Instead, when you paste an image into MS Paint, it can make pixels that have the background color transparent by unchecking the Image menu's Draw Opaque command. To save an image to the clipboard for use by MS Paint, give it a white background and then use that as the background in MS Paint. (In the horrible Ribbonized versions, open the Select tool and select "Transparent selection.")
Many other programs, such as Word, do understand transparency. To paste an image that has a transparent background in Word, use the Paste Special command.
To let you paste images in either kind of application, this example saves an image in two formats: a bitmap with a white background and a PNG formatted image with a transparent background.
The example lets you click and drag to select an irregular area. The GetSelectedArea method returns a bitmap that holds the selected area cut out of a picture. See the post Copy an irregular area from one picture to another in C# to see a description of the basic method.
The following code shows how the program uses the GetSelectedArea method to save images to the clipboard.
// Stop selecting the area.
private void picSource_MouseUp(object sender, MouseEventArgs e)
{
Selecting = false;
// Create a DataObject to hold data
// in different formats.
IDataObject data_object = new DataObject();
// Add a BMP with a white background to the DataObject.
Bitmap bm_white = GetSelectedArea(
picSource.Image, Color.White, Points);
data_object.SetData(DataFormats.Bitmap, bm_white);
// Add a PNG with a transparent background to the DataObject.
Bitmap bm_transparent = GetSelectedArea(
picSource.Image, Color.Transparent, Points);
MemoryStream ms = new MemoryStream();
bm_transparent.Save(ms, ImageFormat.Png);
data_object.SetData("PNG", false, ms);
// Place the data on the clipboard.
Clipboard.Clear();
Clipboard.SetDataObject(data_object, true);
// Let the user know we did something.
System.Media.SystemSounds.Beep.Play();
}
If you're saving text or even a single image to the clipboard, you don't need to use a DtaaObject. Instead you can use the Clipboard object's SetText and SetImage methods. This example saves images in two formats, bitmap and PNG, so it can't use those methods.
To save data in multiple formats, you need to use a DataObject. The code starts by creating a DataObject to hold the data that it will add to the clipboard. It then uses GetSelectedArea to get the selected area with a white background. It calls the DataObject's SetData method to add that image to the DataObject.
The code then gets the selected area again, this time with a transparent background. The clipboard's bitmap format doesn't save transparency data, so the code saves PNG format data instead. It creates a memory stream and makes the bitmap write itself into the stream. The code then uses the DataObject's SetData method to add the memory stream to the DataObject with the PNG format.
Next the program clears the clipboard and calls the Clipboard object's SetDataObject method to add the DataObject to it. The evnet handler finishes by ringing the bell so you know it did something.
Download the example to experiment with it and to see additional details.
|