How to convert jpg,bitmap format image into vector format in c#

8.1k Views Asked by At

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

3

There are 3 best solutions below

0
On

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

1
On

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

0
On
 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);
        }