为什么资源中名为blank.emf
的EMF最终保存为位图?
private void button1_Click(object sender, EventArgs e)
{
Metafile emf = null;
using (var ms = new MemoryStream(Properties.Resources.blank))
{
emf = new Metafile(ms);
}
emf.Save("C:\\Users\\chrisd\\Documents\\emfbitmap1.emf",
ImageFormat.Emf);
}
emfbitmap1.emf
是位图,而不是EMF。我怀疑它是从Properties.Resources检索时转换为位图。它绝对是文件系统上的EMF。
答案 0 :(得分:0)
通过Save
操作将EMF转换为位图。
由于this answer,可以成功编写EMF,只需稍作修改。
using System.Runtime.InteropServices;
包括在课堂上
[DllImport("gdi32.dll")]
internal static extern uint GetEnhMetaFileBits(IntPtr hemf,
uint cbBuffer, byte[] lpbBuffer);
[DllImport("gdi32.dll")]
internal static extern bool DeleteEnhMetaFile(IntPtr hemf);
并修改代码
private void button1_Click(object sender, EventArgs e)
{
Metafile emf = null;
using (var ms = new MemoryStream(Properties.Resources.blank))
{
emf = new Metafile(ms);
}
IntPtr h = emf.GetHenhmetafile();
uint size = GetEnhMetaFileBits(h, 0, null);
byte[] data = new byte[size];
GetEnhMetaFileBits(h, size, data);
using (FileStream w = File.
Create("C:\\Users\\chrisd\\Documents\\emfbitmap1.emf"))
{
w.Write(data, 0, (int)size);
}
DeleteEnhMetaFile(h);
}
当然,如果不需要在内存中安装EMF,则可以直接将其写入光盘。 E.g。
private void button1_Click(object sender, EventArgs e)
{
File.WriteAllBytes("C:\\Users\\chrisd\\Documents\\emfbitmap1.emf",
Properties.Resources.blank);
}