注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

widebright的个人空间

// 编程和生活

 
 
 

日志

 
 

.Net代码把bmp jpg等格式图片转换成wmf图片格式  

2013-05-21 13:56:49|  分类: 程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

一个很古老的工具程序需要wmf图片文件,结果发现电脑内置的程序,画图这些都不能做这个转换。

估计这个格式都被微软淘汰了啊? 默认的gdi+ 是支持这个转换的,系统 也不提供一个接口。

网上找到一个.Net的 调用GDI+ 来做转换的代码。 试了一下visual stuido 2010 + windows xp是可以的。

很简单的几行代码


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;
using System.Drawing.Imaging;

namespace bmp2wmf
{
public partial class Form1 : Form
{

// Ensures that the metafile maintains a 1:1 aspect ratio
private const int MM_ISOTROPIC = 7;
// Allows the x-coordinates and y-coordinates of the metafile to be adjusted
// independently
private const int MM_ANISOTROPIC = 8;
// Specifies the flags/options for the unmanaged call to the GDI+ method
// Metafile.EmfToWmfBits().
private enum EmfToWmfBitsFlags
{
// Use the default conversion
EmfToWmfBitsFlagsDefault = 0x00000000,
// Embedded the source of the EMF metafiel within the resulting WMF
// metafile
EmfToWmfBitsFlagsEmbedEmf = 0x00000001,
// Place a 22-byte header in the resulting WMF file. The header is
// required for the metafile to be considered placeable.
EmfToWmfBitsFlagsIncludePlaceable = 0x00000002,
// Don't simulate clipping by using the XOR operator.
EmfToWmfBitsFlagsNoXORClip = 0x00000004
};

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

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

public static byte[] GetWmfByteArray(System.Drawing.Imaging.Metafile mf)
{
// Get context handle to metafile object. If Graphics.FromImage has been used on the metafile the Graphics object must be disposed at this time.
IntPtr hEmf = mf.GetHenhmetafile();
try
{
// Get the length of the buffer
uint length = GdipEmfToWmfBits(hEmf, 0, null, MM_ISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsIncludePlaceable);
byte[] buffer = new byte[length];
// Get the contents of the buffer
GdipEmfToWmfBits(hEmf, length, buffer, MM_ISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsIncludePlaceable);
return buffer;
}
finally
{
// Release the hEmf
DeleteEnhMetaFile(hEmf);
}
}

private void button1_Click(object sender, 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:\Documents and Settings\Bright\Desktop\recv_reuest.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:\\Documents and Settings\\Bright\\Desktop\\recv_reuest.wmf");
DeleteMetaFile(hmf);
DeleteEnhMetaFile(_hEmf);
}
}
}



  评论这张
 
阅读(675)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017