Click here to Skip to main content
15,503,367 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
I try to create a vector graphic with c# Metafile class.
The following code produces only raster graphics.

How a vector graphic can be created?

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

class CreateMetafileMemory : Form
{
    MemoryStream ms = new MemoryStream();
    Graphics g;

    public static void Main()
    {
        Application.Run(new CreateMetafileMemory());

    }

    public CreateMetafileMemory()
    {
        Graphics grfx = CreateGraphics();
        MemoryStream ms = new MemoryStream();
        IntPtr ipHdc = grfx.GetHdc();

        Metafile mf = new Metafile(ms, ipHdc);
        grfx.ReleaseHdc(ipHdc);
        grfx.Dispose();
        grfx = Graphics.FromImage(mf);

        grfx.FillEllipse(Brushes.Gray, 0, 0, 100, 100);
        grfx.DrawEllipse(Pens.Black, 0, 0, 100, 100);
        grfx.DrawArc(new Pen(Color.Red, 10), 20, 20, 60, 60, 30, 120);
        grfx.Dispose();
        mf.Save(@"C:\file.emf", ImageFormat.Emf);
        mf.Save(@"C:\file.wmf", ImageFormat.Wmf);
    }
}


All the two files which will be produced are PNG-Files. You can verify this by an Hex-Editor.
Posted
Updated 22-Apr-11 12:30pm
v3

A WMF file is classed as a Vector Graphics format, so what is it that makes you say it is producing Raster Graphics?
 
Share this answer
 
Comments
Member 7865133 22-Apr-11 17:30pm    
All the two files which will be produced are PNG-Files. You can verify this by an Hex-Editor.
Henry Minute 23-Apr-11 7:43am    
I wasn't aware of that. Good to know.

I found a possible solution although I haven't tested it. --> http://stackoverflow.com/questions/5270763/convert-an-image-into-wmf-with-net.
Sergey Alexandrovich Kryukov 22-Apr-11 19:15pm    
Henry, OP is right. This is a kind of defect in .NET library: saving of WMF or EMF produces PNG. The solution does exist; I need to dig it out.
--SA
C# Image save Methode does only support to save in raster graphics. No writing of vector graphics is supported.
The solution is to use unmanaged code of the gdi32.dll.

Following some example codes (german wegsite)

Write WMF-Files (will loose transparency)
http://mk85.de/?p=158

Write EMF-Files (hold transparency)
http://mk85.de/?p=165
 
Share this answer
 
v4
Comments
Da_Hero 18-Apr-17 8:21am    
The links are offline at 04'2017. Anybody has saved the information found there? (Waybackmachine does not have the page archived :( )
Member 15084616 15-Aug-21 16:53pm    
https://web.archive.org/web/20131228231728/http://mk85.de/?p=165

Sure it has :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900