将图像转换成WMF与.NET? [英] Convert an image into WMF with .NET?
问题描述
有大量的转换WMF到像一个位图的例子: <一href="http://stackoverflow.com/questions/785837/reliable-wmf-wmf-to-pixel-based-image-conversion">Reliable .WMF / WMF到基于像素的图像转换
There are plenty of examples of converting a wmf into a bitmap like: Reliable .wmf/wmf to Pixel based image conversion
但我需要反向操作。我不看的向量化。我只是想嵌入内部WMF文件的图像,而无需理会位和WMF格式的字节。我需要.NET preferably在C#中的解决方案。
But I need the reverse operation. I do not look for a vectorizer. I just want to embed a picture inside a wmf file without having to bother about the bits and bytes of the wmf format. I need a solution for .NET preferably in C#.
我首先认为这会做的工作:
I first thought this would do the job:
using (Image img = Image.FromFile (path)) {
img.Save (myStream, System.Drawing.Imaging.ImageFormat.Wmf);
}
但这种抱怨在运行时,该连接codeR为空。在哪里/我怎样才能建立这样的连接codeR?我并不需要一个复杂的,只是一个包装图像插入WMF。是否有对WMF支持的格式一些要求?我想,PNG和BMP的支持,但GIF也支持?
But this complains at runtime that the encoder is null. Where/How can I build such an encoder? I do not need a complicated one, just one that wraps an image into a wmf. Are there some requirements on the supported formats in WMF? I suppose png and bmp are supported but is gif also supported?
推荐答案
从的这里:
当您使用Save方法来保存 图形图像作为Windows图元文件 格式(WMF)或增强型图元文件 格式(EMF)文件,产生的文件 保存为可移植网络 图形(PNG)文件代替。本 出现问题的原因GDI + .NET Framework的组件执行 没有一间codeR,您可以使用 将文件保存为.WMF或.EMF文件。
When you use the Save method to save a graphic image as a Windows Metafile Format (WMF) or Enhanced Metafile Format (EMF) file, the resulting file is saved as a Portable Network Graphics (PNG) file instead. This behavior occurs because the GDI+ component of the .NET Framework does not have an encoder that you can use to save files as .wmf or .emf files.
不过,我想你已经做到那一步:)
But I guess you already got that far :)
<一个href="http://www.switchonthe$c$c.com/tutorials/wpf-tutorial-getting-from-wpf-to-a-metafile-and-onto-the-clipboard"相对=nofollow>这里有人把一个位图一个FileStream。
Here someone is putting a bitmap in a FileStream.
metafileStream = MakeMetafileStream(gdiBitmap);
与MakeMetafileStream()的存在:
with MakeMetafileStream() being:
private static MemoryStream MakeMetafileStream(Bitmap image)
{
Graphics graphics = null;
Metafile metafile= null;
var stream = new MemoryStream();
try
{
using (graphics = Graphics.FromImage(image))
{
var hdc = graphics.GetHdc();
metafile= new Metafile(stream, hdc);
graphics.ReleaseHdc(hdc);
}
using (graphics = Graphics.FromImage(metafile))
{ graphics.DrawImage(image, 0, 0); }
}
finally
{
if (graphics != null)
{ graphics.Dispose(); }
if (metafile!= null)
{ metafile.Dispose(); }
}
return stream;
}
有趣的东西。 但作为对EN codeR事情...
Interesting stuff. But as to the encoder thing...
这里黄文雄从微软发布的这个非托管方式:
Here Peter Huang from MS posted this unmanaged approach:
[DllImport("gdiplus.dll")]
private static extern uint GdipEmfToWmfBits (IntPtr _hEmf, uint _bufferSize,
byte[] _buffer, int _mappingMode, EmfToWmfBitsFlags _flags);
[DllImport("gdi32.dll")]
private static extern IntPtr SetMetaFileBitsEx (uint _bufferSize,
byte[] _buffer);
[DllImport("gdi32.dll")]
private static extern IntPtr CopyMetaFile (IntPtr hWmf,
string filename);
[DllImport("gdi32.dll")]
private static extern bool DeleteMetaFile (IntPtr hWmf);
[DllImport("gdi32.dll")]
private static extern bool DeleteEnhMetaFile (IntPtr hEmf);
private void button4_Click(object sender, System.EventArgs e)
{
Graphics g= this.CreateGraphics();
IntPtr hDC = g.GetHdc();
Metafile mf = new Metafile(hDC,EmfType.EmfOnly);
g.ReleaseHdc(hDC);
g.Dispose();
g=Graphics.FromImage(mf);
//Pen p = new Pen(Color.White,5);
g.DrawArc(Pens.Black,0,0,200,200,0,360);
//g.DrawImage(Bitmap.FromFile(@"c:\temp\test.bmp"),0,0);
g.Dispose();
IntPtr _hEmf= mf.GetHenhmetafile();
uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC,
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
byte[] _buffer = new byte[_bufferSize];
GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC,
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
IntPtr hmf = SetMetaFileBitsEx(_bufferSize, _buffer);
CopyMetaFile(hmf, "C:\\ConvertedMetafile.wmf");
DeleteMetaFile(hmf);
DeleteEnhMetaFile(_hEmf);
}
希望这将让你有:)
Hope this'll get you there :)
这篇关于将图像转换成WMF与.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!