2011-10-13 21 views
6

ExportString可以導出EMF或GIF嗎?在這個演示streamoutput.emf不知何故被錯位:使用ExportString轉換圖形

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工作,我也許可以用它通過網絡鏈路,例如轉移電磁場

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); 

回答

8

通過它的外觀,Write包含字符串寫入streamfile的引號,所以輸出文件的東西,如"GIF....,而不是僅僅GIF...開始。當使用BinaryWrite而不是Write它似乎工作。例如

file = ExportString[graphic, "GIF"]; 
stream = OpenWrite["streamoutput.gif", BinaryFormat -> True]; 
BinaryWrite[stream, file]; 
Close[stream]; 
Import["streamoutput.gif"] 

產生

streamoutput

所以ExportString並至少產生了一個GIF有效的字符串。我沒有窗戶,所以我無法測試EMF。

+3

我確認此解決方案也適用於Windows上的EMF。 – WReach

+0

謝謝,海克。 EMF也起作用。 –