用C#做个读取矢量图wmf的小程序,越简单越好,不要gdi+,
只要能读取,显示,并且有放大缩小功能即可
请大家帮忙,谢谢

解决方案 »

  1.   

    基本简介
    图元文件。图元文件的扩展名包括.wmf和.emf两种。
    它们是属于矢量类图形,是由简单的线条和封闭线条(图形)组成的矢量图,其主要特点是文件非常小,可以任意缩放而不影响图像质量。
    Wmf是Windows Metafile 的缩写,简称图元文件,它是微软公司定义的一种Windows​平台下的图形文件格式。wmf格式文件的特点如下:
    1. wmf格式文件是Microsoft Windows操作平台所支持的一种图形格式文件,目前,其它操作系统尚不支持这种格式,如Unix、Linux等。
      它们是属于矢量类图形,是由简单的线条和封闭线条(图形)组成的矢量图,其主要特点是文件非常小,可以任意缩放而不影响图像质量。
    它们是属于矢量类图形,是由简单的线条和封闭线条(图形)组成的矢量图,其主要特点是文件非常小,可以任意缩放而不影响图像质量。
    2. 与bmp格式不同,wmf格式文件是设备无关的,即它的输出特性不依赖于具体的输出设备。
    3. 其图象完全由Win32 API所拥有的GDI函数来完成。
    4. wmf格式文件所占的磁盘空间比其它任何格式的图形文件都要小得多。
    5. 在建立图元文件时,不能实现即画即得,而是将GDI调用记录在图元文件中,之后,在GDI环境中重新执行,才可显示图象。
    6. 显示图元文件的速度要比显示其它格式的图象文件慢,但是它形成图元文件的速度要远大于其它格式。
    不知道你 不要gdi+ 的依据是什么?
      

  2.   

    看了一个C#操作wmf矢量图的,很简单的,关键就是自己没做过,放大缩小如何来弄....哎
      

  3.   

    https://msdn.microsoft.com/zh-cn/library/vs/alm/system.drawing.imaging.metafile.playrecord(v=vs.80)
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Windows.Forms;// for Marshal.Copy
    using System.Runtime.InteropServices; public class Form1 : Form
    {
        private Metafile metafile1;
        private Graphics.EnumerateMetafileProc metafileDelegate;
        private Point destPoint;
        public Form1()
        {
            metafile1 = new Metafile(@"C:\Test.wmf");
            metafileDelegate = new Graphics.EnumerateMetafileProc(MetafileCallback);
            destPoint = new Point(20, 10);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.EnumerateMetafile(metafile1, destPoint, metafileDelegate);
        }
        private bool MetafileCallback(
           EmfPlusRecordType recordType,
           int flags,
           int dataSize,
           IntPtr data,
           PlayRecordCallback callbackData)
        {
            byte[] dataArray = null;
            if (data != IntPtr.Zero)
            {
                // Copy the unmanaged record to a managed byte buffer 
                // that can be used by PlayRecord.
                dataArray = new byte[dataSize];
                Marshal.Copy(data, dataArray, 0, dataSize);
            }        metafile1.PlayRecord(recordType, flags, dataSize, dataArray);        return true;
        }    static void Main()
        {
            Application.Run(new Form1());
        }
    }
      

  4.   

    这个就是上面代码,很简单,现在问题是如何放大缩小,,可难了using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;using System.Drawing.Imaging;
    using System.Runtime.InteropServices;namespace testWmf
    {
        public partial class Form2 : Form
        {
            private Metafile metafile1;
            private Graphics.EnumerateMetafileProc metafileDelegate;
            private Point destPoint;
            private int initX = 0;
            private int initY = 0;        public Form2()
            {
                InitializeComponent();
                metafile1 = new Metafile(@"F:\tmp\testDog\testWmf\641单元601室.wmf");//emf,emf+都可以            metafileDelegate = new Graphics.EnumerateMetafileProc(MetafileCallback);
                destPoint = new Point(initX, initY);        }        private void Form2_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.EnumerateMetafile(metafile1, destPoint, metafileDelegate);
            }        private bool MetafileCallback( EmfPlusRecordType recordType, int flags, int dataSize, IntPtr data, PlayRecordCallback callbackData)
            {
                byte[] dataArray = null;
                if (data != IntPtr.Zero)
                {
                    dataArray = new byte[dataSize];
                    Marshal.Copy(data, dataArray, 0, dataSize);
                }            metafile1.PlayRecord(recordType, flags, dataSize, dataArray);
                return true;
            }
        }
    }
      

  5.   

    不知是什么原因,我这里无法执行你的代码(VS2010)在 Metafile(文件) 处出现异常
    所以只能给另一种显示方法namespace testWmf
    {
        public partial class Form2 : Form
        {
            private Metafile metafile1;
            private Graphics.EnumerateMetafileProc metafileDelegate;
            private Point destPoint;
            private int initX = 0;
            private int initY = 0;        public Form2()
            {
                InitializeComponent();
                //metafile1 = new Metafile(@"641单元601室.wmf");//emf,emf+都可以            //metafileDelegate = new Graphics.EnumerateMetafileProc(MetafileCallback);
                //destPoint = new Point(initX, initY);        }
            //生成矢量图
            protected override void OnLoad(EventArgs e)
            {
                string filePath = @"face.emf";
                Bitmap bmp = new Bitmap(220, 220);
                Graphics gs = Graphics.FromImage(bmp);
                Metafile mf = new Metafile(filePath, gs.GetHdc());
                Graphics g = Graphics.FromImage(mf);
                g.SmoothingMode = SmoothingMode.AntiAlias;            g.FillEllipse(Brushes.Gray, 0, 0, 100, 100);
                g.DrawEllipse(Pens.Black, 0, 0, 100, 100);
                g.DrawArc(new Pen(Color.Red, 10), 20, 20, 60, 60, 30, 120);            g.Save();
                g.Dispose();
                mf.Dispose();
            }
            //绘图
            protected override void OnPaint(PaintEventArgs e)
            {
                var g = e.Graphics;
                var im = Bitmap.FromFile(@"face.emf");
                g.DrawImage(im, 10, 10, im.Width * k, im.Height * k);
                //e.Graphics.EnumerateMetafile(metafile1, destPoint, metafileDelegate);
            }
            //废弃
            private bool MetafileCallback(EmfPlusRecordType recordType, int flags, int dataSize, IntPtr data, PlayRecordCallback callbackData)
            {
                byte[] dataArray = null;
                if (data != IntPtr.Zero)
                {
                    dataArray = new byte[dataSize];
                    Marshal.Copy(data, dataArray, 0, dataSize);
                }            metafile1.PlayRecord(recordType, flags, dataSize, dataArray);
                return true;
            }
            float k = 1;
            //缩小
            private void button2_Click(object sender, EventArgs e)
            {
                k+=0.1f;
                Invalidate();
            }
            //放大
            private void button1_Click(object sender, EventArgs e)
            {
                k -= 0.1f;
                if (k < 1) k = 1;
                Invalidate();
            }
        }
    }