Convert a Windows metafile (wmf file) to a PNG file in C#

A WMF file contains a set of drawing commands that tells a program how to produce an image. This is very useful and allows you to resize the image without producing ugly anti-aliasing effects but sometimes you may want a raster image so you can manipulate its pixels. This example lets you load WMF files and save them as PNG files.

The program uses the following code to load a WMF file.

// Open a WMF file.
private void mnuFileOpen_Click(object sender, EventArgs e)
{
    if (ofdWmfFile.ShowDialog() == DialogResult.OK)
    {
        picImage.Image = new Bitmap(ofdWmfFile.FileName);
        mnuFileSaveAs.Enabled = true;

        ClientSize = new Size(
            picImage.Right + picImage.Left,
            picImage.Bottom + picImage.Left);
    }
}

The code displays a FileOpenDialog to let the user select the WMF file. If the user selects a file and clicks Open, the program loads the file into a Bitmap and displays it.

The program uses the following code to save the loaded image as a PNG file.

// Save the image as a PNG file.
private void mnuFileSaveAs_Click(object sender, EventArgs e)
{
    if (sfdPngFile.ShowDialog() == DialogResult.OK)
    {
        Bitmap bm = (Bitmap)picImage.Image;
        bm.Save(sfdPngFile.FileName, ImageFormat.Png);
    }
}

The program displays a SaveFileDialog to let the user select the file in which to save the image. It then calls the loaded bitmap's Save method passing it the file name and the value ImageFormat.Png to indicate that the image should be saved as a PNG file.

Both WMF and PNG files support transparent pixels. When this program converts a WMF to a PNG file, any transparent pixels are preserved.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.