BLOG.CSHARPHELPER.COM: Copy an irregular area from a picture to the clipboard in C#
Copy an irregular area from a picture to the clipboard in C#
Before explaining how this works, 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.
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 PNG formatted image with a transparent background and a bitmap with a white background.
The example lets the user click and drag to select an irregular area. The GetSelectedAreaImage method returns a bitmap that holds the selected area cut out of a picture. See Copy an irregular area from one picture to another in C# to see a description of the basic method. See this example's code for the details.
GetSelectedAreaImage takes a parameter that gives the color that it should use for the new image's background. The following code shows how the program uses that method to save the image data to the clipboard.
// Copy the selected area to the destination picture. private void btnCopy_Click(object sender, EventArgs e) { // Create a DataObject to hold data // in different formats. IDataObject data_object = new DataObject();
// Make an image with a white background. Bitmap bm_white = GetSelectedAreaImage( picSource.Image, Color.White, Points);
// Add the Bitmap format to the DataObject. data_object.SetData(DataFormats.Bitmap, bm_white);
// Make an image with a transparent background. Bitmap bm_transparent = GetSelectedAreaImage( picSource.Image, Color.Transparent, Points);
// Add the PNG format to the DataObject. 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. MessageBox.Show("Ok"); }
The code first creates a DataObject to hold the data that it will add to the clipboard. It then gets 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 it. The code then uses the DataObject's SetData method to add the memory stream to the DataObject with the PNG format.
Finally the program clears the clipboard and calls the Clipboard's SetDataObject method to add the DataObject's data to it.
Comments