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.SetDataObject(data_object, true);

    // Let the user know we did something.

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 Example   Follow me on Twitter   RSS feed

This entry was posted in graphics, image processing and tagged , , , , , , , , , , , , . Bookmark the permalink.

8 Responses to Copy an irregular area from a picture to the clipboard in C#

  1. Pingback: Paste a PNG format image with a transparent background

  2. Richard Moss says:

    Just been using this to add transparent PNG clipboard support to my application and it works well (thanks!) but I do have one question – is the string “PNG” a standard identifier for this format? (Ie do other applications that copy standard bitmaps + png formats use this ID string) My GoogleFu seems reluctant to find an answer to this question.

    Richard Moss

    • RodStephens says:

      Hmm… I would think so but I’m not sure. PNG is pretty common these days.

      Have you found an application that doesn’t paste it correctly?

      • Richard Moss says:

        Yeah, I know PNG itself is pretty common. I meant the approach you’re taking to store the format on the clipboard – ie the word “PNG” when setting the data. I need to fire up a VM with different graphics tools to test with I guess as the only graphics software on this machine apart from my own is Paint.NET. At least that seems to use “PNG” as a descriptor too, in addition to various custom formats of its own.

        • RodStephens says:

          I think PNG (and png and Png) should be standard. Most of the predefined formats have pretty obvious names. But I’ve only tried it in a few applications so there may be some out there that do it differently.

          You could also try other formats such as DIB and TIF.

  3. Michael Reichert says:

    Oh, thank you Rod, this code helps me a lot after hours of struggling with transparent PNG bitmaps and the clipboard. I tried MemoryStream as a container too, but Word don’t paste it. The hint with IDataObject is great, thank you!

  4. hasan says:

    hi. please i need the project file because i copy the code and paste it in visual studio and I get many errors and I need this program can someone help me .

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.