Convert Graphics Using ExportString

Can ExportString export EMF or GIF? In this demo, streamoutput.emf is somehow distorted:

Quiet[DeleteFile["C:\\Temp\\thisworks.emf"]]; Quiet[DeleteFile["C:\\Temp\\streamoutput.emf"]]; graphic = Graphics[{Thick, Red, Circle[{#, 0}] & /@ Range[4], Black, Dashed, Line[{{0, 0}, {5, 0}}]}]; Export["C:\\Temp\\thisworks.emf", graphic, "EMF"]; file = ExportString[graphic, "EMF"]; stream = OpenWrite["C:\\Temp\\streamoutput.emf", BinaryFormat -> True]; Write[stream, file]; Close[stream]; 

If ExportString worked, I could use it to transfer EMF via NETLink, for example.

 kernel.Compute("ExportString[Graphics[Rectangle[]], \"EMF\"]"); File.WriteAllText("C:\\Temp\\output.emf", kernel.Result.ToString()); 

Adding

Got it.

 kernel.Compute("ExportString[Graphics[Rectangle[]],{\"Base64\",\"EMF\"}]"); byte[] decodedBytes = Convert.FromBase64String(kernel.Result.ToString()); File.WriteAllBytes("C:\\Temp\\output.emf", decodedBytes); 
+6
source share
1 answer

In appearance, Write includes quotes for the file string when writing to stream , so the output file starts with something like "GIF.... , not just GIF... When using BinaryWrite instead of Write it works. For example

 file = ExportString[graphic, "GIF"]; stream = OpenWrite["streamoutput.gif", BinaryFormat -> True]; BinaryWrite[stream, file]; Close[stream]; Import["streamoutput.gif"] 

produces

streamoutput

So, ExportString creates at least a valid string for the GIF. I have no windows, so I can not check EMF.

+8
source

Source: https://habr.com/ru/post/899233/


All Articles