如何在j#中将jpg,位图格式图像转换为矢量格式

时间:2011-11-16 05:58:28

标签: c# image wmf metafile

我尝试将jpg图像转换为矢量格式。我尝试实现此代码,但它是通过exeception

 public void Run()
        {
            Control c = new Control();           
            Graphics grfx = c.CreateGraphics();
           //ReadImage(ImageName) method return the Image to Byte Array
            MemoryStream ms = new MemoryStream(ReadImage(@"E:\Temp\1.jpg"));
            IntPtr ipHdc = grfx.GetHdc();
            Metafile mf = new Metafile(ms,ipHdc);
            grfx.ReleaseHdc(ipHdc);
            grfx.Dispose();
            grfx = Graphics.FromImage(mf);
            mf.Save(@"E:\Temp\file.wmf", ImageFormat.Wmf);//Get Exception on this line
            grfx.Dispose();
        }

执行是:GDI +中发生了一般错误。 请验证我的代码在哪里我犯了错误。 在此先感谢

3 个答案:

答案 0 :(得分:3)

代码不能工作:Afaik Metafile构造函数需要一个已包含一些元文件数据的流(即'.wmf'文件)或为空(对于新的元文件)

您应该创建一个新的图元文件,从中创建图形上下文,将jpeg图像加载到单独的Image对象中,并在元文件上下文中绘制它。然后,您可以将图元文件保存为“.wmf”文件。

我自己没有这样做,但我找到了一个article on CodeProject,它解释了关于元文件创建的许多(棘手的)细节。

但请注意,这不是矢量转换的“真实”位图。它只是将位图嵌入到“.wmf”容器中。例如,如果您尝试调整大小,则会得到与原始jpeg-Image相同的结果(即没有“平滑”缩放)。

答案 1 :(得分:1)

我在应用程序之前遇到了同样的错误,因为将文件写入磁盘而出现了问题

E:\Temp\file.wmf

检查文件的写入权限!我的目录是网络映射内存所以我必须连接目录与pass和username.Make确保父目录存在并确保路径包括文件名和扩展名。如果它不工作尝试以管理员身份运行程序

答案 2 :(得分:0)

 public string Main(Bitmap image)
        {
             string str = "";
            try
            {

                int width = image.Width;
                int height = image.Height;

                Graphics offScreenBufferGraphics;
                Metafile m;
                using (MemoryStream stream = new MemoryStream())
                {
                    using (offScreenBufferGraphics = Graphics.FromImage(image))
                    {
                        IntPtr deviceContextHandle = offScreenBufferGraphics.GetHdc();
                        m = new Metafile(
                        stream,
                        deviceContextHandle,
                        new RectangleF(0, 0, width, height),
                        MetafileFrameUnit.Pixel,
                        EmfType.EmfPlusOnly);
                        offScreenBufferGraphics.ReleaseHdc();
                    }
                }

                using (Graphics g = Graphics.FromImage(m))
                {
                    // Set everything to high quality and Draw image
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    MetafileHeader metafileHeader = m.GetMetafileHeader();
                    g.ScaleTransform(
                      metafileHeader.DpiX / g.DpiX,
                      metafileHeader.DpiY / g.DpiY);
                    g.PageUnit = GraphicsUnit.Pixel;
                    g.SetClip(new RectangleF(0, 0, width, height));
                    Point ulCorner = new Point(0, 0);
                    g.DrawImage(image, 0, 0, width, height);


                }

                // Get a handle to the metafile
                IntPtr iptrMetafileHandle = m.GetHenhmetafile();

                // Export metafile to an image file
                CopyEnhMetaFile(iptrMetafileHandle, @"c:\\Ginko-Bilobatest1234.wmf");
                str = "wmf image successfully generated.";
            }
            catch (Exception ex)
            {
                str = ex.InnerException + ex.Message;
            }
            return str;
            // Delete the metafile from memory
            // DeleteEnhMetaFile(iptrMetafileHandle);
        }