gpt4 book ai didi

c# - 如何显示 Windows 图元文件?

转载 作者:太空狗 更新时间:2023-10-29 17:32:12 25 4
gpt4 key购买 nike

我需要显示一个 Windows Metafile (EMF) 使用 WPF,我该怎么做?

编辑:

我会保持基于矢量的图像。

最佳答案

看看 3rd party library Ab2d.ReadWmf .

更新 #1:概述

首先,this post声明 Microsoft 不打算在 WPF 中支持 EMF 文件。这并不意味着它不能完成,只是他们不会支持他们。

查看 Wikipedia page关于 WMF/EMF 格式,我看到它将 EMF 描述为:

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。提供快速概览 here .这意味着您需要读入 EMF 文件并将 GDI 调用转换为 WPF 调用。 Here's a thread他们讨论过程的地方。对我来说,这听起来像是很多工作。

幸运的是,Microsoft 提供了一个用于读取 Windows 图元文件的界面。看看this thread举个例子,documentation available here ,但这只会让你完成一半,因为它不是 WPF 视觉对象。在这一点上,我认为最简单的解决方案是在您的 WPF 应用程序中创建一个 WinForms 控件并将其托管在 WindowsFormsHost 控件中。

更新 #2:代码示例

在 WPF 应用程序中显示 EMF 文件:

  1. 创建 WinForms 用户控件
  2. 将您的 EMF 文件加载到 MetaFile 对象中,并在 OnPaint 处理程序中绘制它。
  3. 添加对 WindowsFormsIntegration 库的引用
  4. 在 WindowsFormsHost 元素中托管您的 WinForms 控件

用户控件:

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:

<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>

关于c# - 如何显示 Windows 图元文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10147293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com