Can ExportString export an EMF or GIF? In this demo streamoutput.emf somehow gets mangled:
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 might be able to use it to transfer EMFs through NETLink, e.g.
kernel.Compute("ExportString[Graphics[Rectangle[]], \"EMF\"]");
File.WriteAllText("C:\\Temp\\output.emf", kernel.Result.ToString());
Addendum
Got that working.
kernel.Compute("ExportString[Graphics[Rectangle[]],{\"Base64\",\"EMF\"}]");
byte[] decodedBytes = Convert.FromBase64String(kernel.Result.ToString());
File.WriteAllBytes("C:\\Temp\\output.emf", decodedBytes);
By the looks of it, Write includes the quotation marks of the string file when writing to stream, so the output file starts with something like "GIF.... instead of just GIF.... When using BinaryWrite instead of Write it does seem to work. For example
file = ExportString[graphic, "GIF"];
stream = OpenWrite["streamoutput.gif", BinaryFormat -> True];
BinaryWrite[stream, file];
Close[stream];
Import["streamoutput.gif"]
produces

So ExportString does produce a valid string for GIF at least. I don't have windows so I can't test for EMF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With