开发者

Converting graphics with ExportString

开发者 https://www.devze.com 2023-04-13 07:15 出处:网络
Can ExportString export an EMF or GIF? In this demo streamoutput.emf somehow gets mangled: Quiet[DeleteFile[\"C:\\\\Temp\\\\thisworks.emf\"]];

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

Converting graphics with ExportString

So ExportString does produce a valid string for GIF at least. I don't have windows so I can't test for EMF.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号