How can I copy binary data in the clipboard? For example if I pack the number 1 as a 4-byte little-endian integer I want my clipboard to show 00 00 00 01
For text data this is trivial, with the option of having unicode text or ascii text.
Clipboard.SetData(DataFormats.Text, "Some text");
Clipboard.SetData(DataFormats.UnicodeText, "赤");
However for binary data I don't know what to do.
There are actually two ways to do this:
First one, and by far the simplest one: you simply put the byte array into the clipboard. This will automatically serialize the byte array, and deserialize it on retrieve, and all you need to do is check for
typeof(Byte[])
. In fact, this works for any serializable type (and you can make your own classes serializable with the[Serializable]
attribute).Put on clipboard:
Retrieve from clipboard:
If you absolutely want it to be on there as pure raw bytes, another possibility is to put it on the clipboard as MemoryStream. There is no specific type for this in the
DataFormats
list, but since the listed data formats are just strings, you can just make up your own. I used "rawbinary" in the following example.Put on clipboard:
Retrieve from clipboard:
This second type is often used for custom formats; for example, the Office clipboard puts images into the clipboard as PNG byte stream (with identifier "PNG") because the standard clipboard image type lacks transparency support.