Wolfram mathematica 使用ExportString转换图形
ExportString可以导出EMF或GIF吗?在本演示中,streamoutput.emf以某种方式损坏:Wolfram mathematica 使用ExportString转换图形,wolfram-mathematica,Wolfram Mathematica,ExportString可以导出EMF或GIF吗?在本演示中,streamoutput.emf以某种方式损坏: 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},
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];
如果ExportString有效,我可能可以使用它通过NETLink传输EMF,例如
kernel.Compute("ExportString[Graphics[Rectangle[]], \"EMF\"]");
File.WriteAllText("C:\\Temp\\output.emf", kernel.Result.ToString());
附录
让它起作用了
kernel.Compute("ExportString[Graphics[Rectangle[]],{\"Base64\",\"EMF\"}]");
byte[] decodedBytes = Convert.FromBase64String(kernel.Result.ToString());
File.WriteAllBytes("C:\\Temp\\output.emf", decodedBytes);
从外观上看,
Write
在写入stream
时包含字符串文件
的引号,因此输出文件以类似于“GIF…”
的内容开始,而不仅仅是GIF…
。当使用BinaryWrite
而不是Write
时,它看起来确实有效。例如
file = ExportString[graphic, "GIF"];
stream = OpenWrite["streamoutput.gif", BinaryFormat -> True];
BinaryWrite[stream, file];
Close[stream];
Import["streamoutput.gif"]
产生
因此,
ExportString
至少为GIF生成了一个有效字符串。我没有windows,因此无法测试EMF。我确认此解决方案也适用于windows上的EMF。