Adding a png to clipboard

3.9k views Asked by At

I have a simple question. What do I use as a DataFormat when saving a png to the clipboard.

An example that works for Bitmaps is:

DataObject data = new DataObject();
data.SetData(DataFormats.Bitmap, customObject.ToImage(ImageFormat.Bmp));
Clipboard.SetDataObject(data);

My problem:

DataObject data = new DataObject();
data.SetData(DataFormats.**???**, customObject.ToImage(ImageFormat.Png));
Clipboard.SetDataObject(data);
1

There are 1 answers

1
Ritch Melton On BEST ANSWER

You can register any serializable type with the clipboard provider:

        Image image = Image.FromFile(@"D:\Documents\Projects\....\myimage.png");
        DataFormat format = DataFormats.GetDataFormat(typeof (Image).FullName);

        IDataObject dataObj = new DataObject();
        dataObj.SetData(format.Name, image);

        Clipboard.SetDataObject(dataObj);

        IDataObject clipboardObj = Clipboard.GetDataObject();
        Image clipboardImage = (Image)clipboardObj.GetData(format.Name);