dlf*_*dlf 2 c# windows clipboard svg
I have an SVG image stored as a .net XML string. If I write that string to a file, I can load it into SVG editors without any trouble, so I know its contents are good. But what I want to do is place it in the Windows clipboard as the image/svg+xml MIME type. I've tried the following:
string svg = GetSvg();
byte[] bytes = Encoding.UTF8.GetBytes(svg);
Clipboard.SetData("image/svg+xml", svg); // idea 1
Clipboard.SetData("image/svg+xml", bytes); // idea 2
Run Code Online (Sandbox Code Playgroud)
Based on my clipboard viewer tool, both techniques produce (almost) the same result--The XML text is there as expected under image/svg+xml, but it is prefixed with 43 bytes that are definitely not present in svg or bytes:
These bytes differ slightly depending on whether I write the text as a string or a byte array, so I suspect they are some sort of description of the data format. However, no SVG editor I have will accept the result for pasting. Is there more I need to do?
这些额外的字节看起来很像序列化头,所以我四处寻找,最终在该类的 MSDN 文档中找到了这个注释Clipboard(我的粗体):
一个对象必须是可序列化的,才能将其放在剪贴板上。如果将不可序列化的对象传递给 Clipboard 方法,则该方法将失败而不会引发异常。有关序列化的更多信息,请参见 System.Runtime.Serialization。如果您的目标应用程序需要非常特定的数据格式,则在序列化过程中添加到数据的标头可能会阻止应用程序识别您的数据。要保留您的数据格式,请将您的数据作为 Byte 数组添加到 MemoryStream 并将 MemoryStream 传递给 SetData 方法。
这表明了一个明显的行动方针:
string svg = GetSvg();
byte[] bytes = Encoding.UTF8.GetBytes(svg);
MemorySteam stream = new MemoryStream(bytes);
Clipboard.SetData("image/svg+xml", stream);
Run Code Online (Sandbox Code Playgroud)
那行得通!此外,我可以确认,如果您希望同时以 svg 和位图形式将图像推送到剪贴板,我也可以DataObject.SetData()接受MemoryStream。
| 归档时间: |
|
| 查看次数: |
1239 次 |
| 最近记录: |