Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleConvert a Windows metafile (wmf file) to a PNG file in Visual Basic .NET
DescriptionThis example shows how to convert a Windows metafile (wmf file) to a PNG file in Visual Basic .NET
Keywordsgraphics, image processing, wmf file, png file, Windows metafile, metafile, example, example program, Windows Forms programming, Visual Basic .NET, VB.NET
CategoriesGraphics, Graphics
 

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 Sub mnuFileOpen_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles mnuFileOpen.Click
    If (ofdWmfFile.ShowDialog() = DialogResult.OK) Then
        picImage.Image = New Bitmap(ofdWmfFile.FileName)
        mnuFileSaveAs.Enabled = True

        ClientSize = New Size( _
            picImage.Right + picImage.Left, _
            picImage.Bottom + picImage.Left)
    End If
End Sub
 
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 Sub mnuFileSaveAs_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    mnuFileSaveAs.Click
    If (sfdPngFile.ShowDialog() = DialogResult.OK) Then
        Dim bm As Bitmap = DirectCast(picImage.Image, _
            Bitmap)
        bm.Save(sfdPngFile.FileName, ImageFormat.Png)
    End If
End Sub
 
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.

 
 
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated