Converting graphics with ExportString
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开发者_如何学C 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.
精彩评论