【问题标题】:How to convert jpg,bitmap format image into vector format in c#c#中如何将jpg、位图格式的图片转成矢量格式
【发布时间】:2011-12-30 03:33:05
【问题描述】:

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

 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+ 中发生一般错误。 请验证我的代码在哪里出错。 提前致谢

【问题讨论】:

  • 而异常是在哪一行源码中抛出的?
  • 在这条线上我得到了异常 mf.Save(@"E:\Temp\file.wmf", ImageFormat.Wmf);

标签: c# image wmf metafile


【解决方案1】:

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

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

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

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

【讨论】:

  • 这个警告确实让我免于浪费时间......狩猎仍在继续!
【解决方案2】:

我之前在我的应用程序中遇到了同样的错误,问题是因为将文件写入磁盘而发生的,就像你的一样

E:\Temp\file.wmf

检查文件的写入权限!我的目录是网络中的映射内存,所以我必须使用密码和用户名连接目录。确保父目录存在并确保路径包含文件名和扩展名。如果它不起作用,请尝试以管理员身份运行程序

【讨论】:

    【解决方案3】:
     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);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 2018-08-26
      • 1970-01-01
      • 2011-05-16
      相关资源
      最近更新 更多