本文介绍了使用 ExportString 转换图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ExportString 可以导出 EMF 或 GIF 吗?在这个演示中,streamoutput.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];

如果 ExportString 有效,我也许可以使用它通过 NETLink 传输 EMF,例如

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

附录

成功了.

kernel.Compute("ExportString[Graphics[Rectangle[]],{\"Base64\",\"EMF\"}]");
byte[] decodedBytes = Convert.FromBase64String(kernel.Result.ToString());
File.WriteAllBytes("C:\\Temp\\output.emf", decodedBytes);

推荐答案

从外观上看,Write 在写入 file 时包含字符串 file 的引号code>stream,因此输出文件以类似 "GIF.... 的内容开头,而不仅仅是 GIF....使用 BinaryWrite 时 而不是 Write 它似乎确实有效.例如

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"]

生产

所以 ExportString 至少会为 GIF 生成一个有效的字符串.我没有窗户,所以无法测试 EMF.

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

这篇关于使用 ExportString 转换图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 03:16