【问题标题】:.NETLink Graphics producing PNG instead of EMF.NETLink 图形生成 PNG 而不是 EMF
【发布时间】:2011-09-25 00:27:03
【问题描述】:

下面的 C# 代码应该产生一个 EMF,但查看输出(在 Vim 中)显示它是一个 PNG。也许有人在 S.O.知道一个好的解决方法或解决方案。

MathKernel k = new MathKernel();
k.CaptureGraphics = true;
k.GraphicsFormat = "Metafile";
k.Compute("Show[Graphics[{Thick, Blue, Circle[{#, 0}] & /@ Range[4], Black, Dashed, Line[{{0, 0}, {5, 0}}]}]]");
k.Graphics[0].Save("C:\\Temp\\file.emf", System.Drawing.Imaging.ImageFormat.Emf);

到目前为止,我正在考虑将 Show[Graphics...] 包装在 ExportString[... , "EMF"] 中并使用 MathKernel Result 属性收集结果。

附录

MathKernel.Graphics 属性[1] 显然是一种 .Net Graphics 方法,它只处理位图等图像文件,而不是基于矢量图形的增强元文件。

  1. http://reference.wolfram.com/legacy/v7/NETLink/ref/net/Wolfram.NETLink.MathKernel.Graphics.html

增强的元文件可以通过 .NETLink 一次传输一个,但方式如下:

using System;
using System.IO;
using Wolfram.NETLink;

public class Example
{
    public static void Main(String[] args)
    {
        MathKernel k = new MathKernel();
        k.Compute("ExportString[Graphics[{Disk[]}], {\"Base64\", \"EMF\"}]");
        byte[] decodedBytes = Convert.FromBase64String(k.Result.ToString());
        // The transferred EMF can be used or simply written out to file.
        File.WriteAllBytes("C:\\Temp\\file.emf", decodedBytes);
    }
}

【问题讨论】:

  • 您是否从 MathKernel 启动了非交互式 FrontEnd 进程并将 latted 配置为使用前者作为 $FrontEnd?可能 EMF 文件是由 FrontEnd 生成的,没有它就无法创建。另请注意,Mathematica 只能在 Windows 下导出 EMF 文件。
  • @Alexey - 谢谢。 EMF 很可能只在前端产生。 MathKernel.GraphicsFormat 将“元文件”列为输出格式。等着瞧。 reference.wolfram.com/legacy/v7/NETLink/ref/net/…
  • 在该页面上未列出您获得的 PNG 格式。

标签: wolfram-mathematica


【解决方案1】:

这是一个可行的解决方案:

using System;
using Wolfram.NETLink;

public class Example {

 public static void Main(String[] args) {

  MathKernel k = new MathKernel();
  k.Compute("Export[\"c:/users/arnoudb/out.emf\", Graphics[{Disk[]}], \"EMF\"]");
  }

} 

我不知道你为什么考虑这部分:

k.Graphics[0].Save("C:\\Temp\\file.emf", System.Drawing.Imaging.ImageFormat.Emf);

一个 Mathematica 错误,因为 k.Graphics[0] 是一个纯 C# System.Drawing.Image 类。或许你可以澄清一下这部分?

【讨论】:

  • @Arnoud,再次感谢。令人惊讶的是 .Net Graphics 方法/类不处理 EMF,这对于累积的图形很有用。我已经编辑了我的原始帖子,并添加了一种无需将它们写入磁盘即可传输单个 EMF 的方法。
猜你喜欢
  • 2012-02-06
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 2012-12-18
  • 1970-01-01
相关资源
最近更新 更多