c# - Copy SVG image stored as XML string to Windows clipboard as image/svg+xml MIME type -


i have svg image stored .net xml string. if write string file, can load svg editors without trouble, know contents good. want place in windows clipboard image/svg+xml mime type. i've tried 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 

based on clipboard viewer tool, both techniques produce (almost) same result--the xml text there expected under image/svg+xml, prefixed 43 bytes not present in svg or bytes:

clipboard viewer

these bytes differ depending on whether write text string or byte array, suspect sort of description of data format. however, no svg editor have accept result pasting. there more need do?

those bytes looked awful lot serialization header, hunted around , found note in msdn documentation clipboard class (bolding mine):

an object must serializable put on clipboard. if pass non-serializable object clipboard method, method fail without throwing exception. see system.runtime.serialization more information on serialization. if target application requires specific data format, headers added data in serialization process may prevent application recognizing data. preserve data format, add data byte array memorystream , pass memorystream setdata method.

that suggested obvious course of action:

string svg = getsvg(); byte[] bytes = encoding.utf8.getbytes(svg); memorysteam stream = new memorystream(bytes); clipboard.setdata("image/svg+xml", stream); 

that worked! further, can confirm dataobject.setdata() accept memorystream in case looking push image clipboard in both svg , bitmap form @ same time.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -