问题
The C# code below should produce an EMF, but viewing the output (in Vim) shows it to be a PNG. Perhaps someone on S.O. knows a good work-around or solution.
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);
So far I'm considering wrapping Show[Graphics...] in ExportString[... , "EMF"] and collecting the result using the MathKernel Result property.
Addendum
The MathKernel.Graphics property[1] is apparently a .Net Graphics method which only handles image files such as bitmaps, not vector graphic based enhanced metafiles.
- http://reference.wolfram.com/legacy/v7/NETLink/ref/net/Wolfram.NETLink.MathKernel.Graphics.html
Enhanced metafiles can be transferred through .NETLink one at a time though, in the following manner:
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);
}
}
回答1:
Here is a working solution:
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\"]");
}
}
I am not sure why you consider this part:
k.Graphics[0].Save("C:\\Temp\\file.emf", System.Drawing.Imaging.ImageFormat.Emf);
a Mathematica bug, since k.Graphics[0] is a pure C# System.Drawing.Image class. Perhaps you can clarify this part a bit?
来源:https://stackoverflow.com/questions/7542828/netlink-graphics-producing-png-instead-of-emf