将二进制数据复制到剪贴板 [英] Copy binary data to clipboard

查看:97
本文介绍了将二进制数据复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在剪贴板中复制二进制数据?例如,如果我将数字1打包为4字节的Little-Endian整数,则希望剪贴板显示 00 00 00 01

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

对于文本数据而言,这很简单,可以选择使用unicode文本还是ascii文本。

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:

第一种,到目前为止是最简单的一种:您只需将字节数组放入剪贴板。这将自动序列化字节数组,并在检索时反序列化它,所有您需要做的就是检查 typeof(Byte [])。实际上,这适用于任何可序列化的类型(您可以使用 [Serializable] 属性)。

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).

放在剪贴板上:

public void PutBytesOnClipboardObj(Byte[] byteArr)
{
    DataObject data = new DataObject();
    // Can technically just be written as "SetData(byteArr)", but this is more clear.
    data.SetData(typeof(Byte[]), byteArr);
    // The 'copy=true' argument means the data will remain available
    // after the program is closed.
    Clipboard.SetDataObject(data, true);
}

从剪贴板中检索:

public byte[] GetBytesFromClipboardObj()
{
    DataObject retrievedData = Clipboard.GetDataObject() as DataObject;
    if (retrievedData == null || !retrievedData.GetDataPresent(typeof(Byte[])))
        return null;
    return retrievedData.GetData(typeof(Byte[])) as Byte[];
}

如果您绝对希望将其作为纯原始字节存在,那是另一种可能性是将其作为MemoryStream放在剪贴板上。 DataFormats 列表中没有特定的类型,但是由于列出的数据格式只是字符串,因此您可以自己编写。在下面的示例中,我使用了 rawbinary。

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.

放在剪贴板上:

public void PutBytesOnClipboardRaw(Byte[] byteArr)
{
    DataObject data = new DataObject();
    using (MemoryStream memStream = new MemoryStream())
    {
        memStream.Write(byteArr, 0, byteArr.Length);
        data.SetData("rawbinary", false, memStream);
        // The 'copy=true' argument means the MemoryStream
        // can be safely disposed after the operation.
        Clipboard.SetDataObject(data, true);
    }
}

从剪贴板中检索:

public Byte[] GetBytesFromClipboardRaw()
{
    DataObject retrievedData = Clipboard.GetDataObject() as DataObject;
    if (retrievedData == null || !retrievedData.GetDataPresent("rawbinary"))
        return null;
    MemoryStream byteStream = retrievedData.GetData("rawbinary") as MemoryStream;
    if (byteStream == null)
        return null;
    return byteStream.ToArray();
}

第二种类型通常用于自定义格式;例如,Office剪贴板将图像作为PNG字节流(带有标识符 PNG)放入剪贴板,因为标准剪贴板图像类型缺乏透明度支持。

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.

这篇关于将二进制数据复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆