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