如何显示Windows图元文件? [英] How to display Windows Metafile?

查看:430
本文介绍了如何显示Windows图元文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示使用WPF一个 Windows图元文件(EMF),我该怎么办?

I need to display a Windows Metafile (EMF) using WPF, how can I do?

我倒是保持图像的基于矢量的。

I'd to keep the image vector-based.

推荐答案

看看的第三方库Ab2d.ReadWmf

更新#1:概述

首先,<一个href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7010cefa-e596-45af-b193-40206451dd90/">this帖子指出,微软并不打算在WPF支持EMF文件。这并不意味着它不能做的,只是他们不会支持他们。

First off, this post states that Microsoft does not intend support EMF files in WPF. That doesn't mean it can't be done, just that they will not support them.

纵观维基百科页面有关的WMF / EMF格式我看到它描述EMF为:

Looking at the Wikipedia page about the WMF/EMF format I see that it describes EMF as:

实质上,WMF文件存储了具有要发出到Windows图形设备接口功能调用的列表(GDI)层在屏幕上显示的图像。由于一些GDI函数接受指向的回调函数的错误处理,WMF文件可能会错误地包括可执行code。

Essentially, a WMF file stores a list of function calls that have to be issued to the Windows Graphics Device Interface (GDI) layer to display an image on screen. Since some GDI functions accept pointers to callback functions for error handling, a WMF file may erroneously include executable code.

如果你用WPF工作多,你知道,WPF比GDI根本的不同。快速预览可用<一个href="http://www.leadtools.com/help/leadtools/v15/dh/wpf/to/leadtools.wpf.topics~leadtools.wpf.topics.differencesbetweengdiandwpf.html">here.这意味着你将需要阅读的EMF文件和翻译GDI调用WPF电话。 <一href="http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/4848cd92-5b15-4d25-adbd-7b30ef3677bc">Here's一个线程,他们讨论的过程。这听起来像一个大量的工作给我。

If you've worked with WPF much you know that WPF is fundamentally different than GDI. A quick overview is available here. This means that you'll need to read in your EMF file and translate the GDI calls to WPF calls. Here's a thread where they discuss the process. That sounds like a lot of work to me.

幸运的是,微软提供了一个接口,用于读取Windows图元。看看<一href="http://social.msdn.microsoft.com/Forums/en/winforms/thread/bce89dc7-78f8-4965-8d01-a9f7afc86544">this线程一个例子,可以在这里的文档,但是这只会让你中途有,因为它不是一个WPF视觉。在这一点上我觉得最简单的解决办法是在你的WPF应用程序创建一个WinForms控制并将其驻留在WindowsFormsHost控件中。

Luckily, Microsoft provides an interface for reading in Windows Metafiles. Take a look at this thread for an example and the documentation available here, but this will only get you half way there since it's not a WPF Visual. At this point I think the easiest solution would be to create a WinForms control in your WPF app and host it inside a WindowsFormsHost control.

更新#2:code样品

要在WPF应用程序显示一个EMF文件:

To display an EMF file in a WPF application:

  1. 创建一个WinForms用户控件
  2. 在加载了EMF文件转换成图元文件对象,然后在OnPaint处理绘制。
  3. 添加引用库WindowsFormsIntegration程序
  4. 主机您的WinForms控制WindowsFormsHost元素放在

用户控件:

public partial class UserControl1 : UserControl
{
     private Metafile metafile1;

     public UserControl1()
     {
         InitializeComponent();
         metafile1 = new Metafile(@"C:\logo2.emf");
     }

     protected override void OnPaint(PaintEventArgs e)
     {
         e.Graphics.DrawImage(metafile1, 0, 0);
     }
}

XAML:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:app="clr-namespace:WpfApplication1" 
    Title="MainWindow" Height="200" Width="200">

     <Grid>
         <WindowsFormsHost>
             <app:UserControl1/>
         </WindowsFormsHost>
     </Grid>
 </Window>

这篇关于如何显示Windows图元文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆