如何在c#中将jpg、bitmap格式图像转换为矢量格式

发布于 2024-12-16 18:38:06 字数 730 浏览 1 评论 0原文

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

I try to convert the jpg image into vector format.I try to implement this code but it's through the 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();
        }

Exeception is :A generic error occurred in GDI+.
Please verify my code where i did the mistake.
Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

闻呓 2024-12-23 18:38:06

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

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

我自己没有这样做,但我发现了一篇 文章CodeProject 上解释了有关图元文件创建的许多(棘手的)细节。

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

The code cannot work: Afaik the Metafile constructor expects a stream which already contains some metafile data (i.e. an '.wmf'-File) or is empty (for new metafiles)

You should create a new metafile, create a graphics context from it, load the jpeg image into a separate Image object and draw it on the metafile context. Then you can save the metafile as ".wmf" files.

I didn't do this myself, but i found an article on CodeProject which explains many of the (tricky) details about metafile creation.

Note however that this is not a "real" bitmap to vector conversion. It merely embeds the bitmap into an ".wmf" container. If you try to resize it for example, you'll get the same results as for the original jpeg-Image (i.e. no "smooth" scaling).

风铃鹿 2024-12-23 18:38:06

我之前在我的应用程序中遇到了同样的错误,问题是由于将文件写入磁盘而发生的,因此您的应用程序

E:\Temp\file.wmf

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

I got the same error before in my application the problem occured becouse of writing file to disk so as yours

E:\Temp\file.wmf

check the file permission for write! My directory was Mapped Memory in network so i had to connect directory with pass and username.Make sure that the parent directory exists and Ensure that path includes both the filename and extension.if its not work try to run program as admin

む无字情书 2024-12-23 18:38:06
 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);
        }
 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);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文