I’m trying to get IDataObject from clipboard, and setting it back after I’m done using clipboard.
// keep clipboard's data to restore it later.
IDataObject tmpClipboard = Clipboard.GetDataObject();
Clipboard.Clear();
// using the clipboard to do my things
// ...
// restore the clipboard to previous state
Clipboard.Clear();
Clipboard.SetDataObject(tmpClipboard);
This code block generally works for simple clipboard data.
However, for some data types in the clipboard (ex: bitmapsource), calling Clipboard.SetDataObject()
will throw a COM exception:
Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC)
Is it even possible to handle general-purpose clipboard data types and keep it, so I could restore it to the clipbard later?
2