Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 that working.

kernel.Compute("ExportString[Graphics[Rectangle[]],{\"Base64\",\"EMF\"}]");
byte[] decodedBytes = Convert.FromBase64String(kernel.Result.ToString());
File.WriteAllBytes("C:\\Temp\\output.emf", decodedBytes);
like image 598
Chris Degnen Avatar asked Oct 13 '11 14:10

Chris Degnen


1 Answers

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

streamoutput

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

like image 54
Heike Avatar answered Nov 18 '22 22:11

Heike