Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » GDI+ » General » Downloads
 
Add your own
alternative version

Building a Simple Image Conversion Utility

, 13 Sep 2006
This article describes a very easy approach to building an image conversion utility that will permit the user to open a supported image type and convert it to another supported image type.
imageconverter.zip
ImageConverter
ImageConverter
bin
Release
ImageConverter.exe
Properties
Settings.settings
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

namespace ImageConverter
{
    public partial class Form1 : Form
    {
        // local variable declarations
        string CurrentFile;
        Image img;

        public Form1()
        {
            InitializeComponent();
        }

        // Show open file dialog to allow user to open an image file
        // for display in the application
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Open Image File";
            openFileDialog1.Filter = "Bitmap Files|*.bmp" +
                "|Enhanced Windows MetaFile|*.emf" + 
                "|Exchangeable Image File|*.exif" +
                "|Gif Files|*.gif|Icons|*.ico|JPEG Files|*.jpg" + 
                "|PNG Files|*.png|TIFF Files|*.tif|Windows MetaFile|*.wmf";
            openFileDialog1.DefaultExt = "bmp";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();

            if (openFileDialog1.FileName == "")
                return;

            CurrentFile = openFileDialog1.FileName.ToString();

            img = Image.FromFile(openFileDialog1.FileName);
            pictureBox1.Image = img;
        }

        // Exit the application
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // Conversion function follow to convert to any supported format;
        // there is one method for each file type in this example

        // Convert to Bitmap
        private void bitmapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile);
            newName = newName + ".bmp";

            try
            {
                img.Save(newName, ImageFormat.Bmp);
            }
            catch
            {
                MessageBox.Show("Failed to save image to bitmap.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // Convert to EXIF
        private void exifToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile);
            newName = newName + ".exif";

            try
            {
                img.Save(newName, ImageFormat.Exif);
            }
            catch
            {
                MessageBox.Show("Failed to save image to EXIF format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // Convert to EMF
        private void emfToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile);
            newName = newName + ".emf";

            try
            {
                img.Save(newName, ImageFormat.Emf);
            }
            catch
            {
                MessageBox.Show("Failed to save image to EMF format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // Convert to GIF
        private void gIFFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile);
            newName = newName + ".gif";

            try
            {
                img.Save(newName, ImageFormat.Gif);
            }
            catch
            {
                MessageBox.Show("Failed to save image to GIF format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // Convert to Icon
        private void iconFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile);
            newName = newName + ".ico";

            try
            {
                img.Save(newName, ImageFormat.Icon);
            }
            catch
            {
                MessageBox.Show("Failed to save image to ICO format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // Convert to JPEG
        private void jPEGFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile);
            newName = newName + ".jpg";

            try
            {
                img.Save(newName, ImageFormat.Jpeg);
            }
            catch
            {
                MessageBox.Show("Failed to save image to JPEG format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // Convert to PNG
        private void pNGFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile);
            newName = newName + ".png";

            try
            {
                img.Save(newName, ImageFormat.Png);
            }
            catch
            {
                MessageBox.Show("Failed to save image to PNG format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // Convert to TIFF
        private void tIFFFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile);
            newName = newName + ".tif";

            try
            {
                img.Save(newName, ImageFormat.Tiff);
            }
            catch
            {
                MessageBox.Show("Failed to save image to TIFF format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // Convert to WMF
        private void windowsMetafileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile);
            newName = newName + ".wmf";

            try
            {
                img.Save(newName, ImageFormat.Wmf);
            }
            catch
            {
                MessageBox.Show("Failed to save image to WMF format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

salysle
Software Developer (Senior)
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 13 Sep 2006
Article Copyright 2006 by salysle
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid