Project for Image Conversion

Raj.Trivedi
Posted by in C# category on for Beginner level | Points: 250 | Views : 1531 red flag

In this article we will see how we can convert Images from one form to another.

Introduction


Recently I was wondering if we can change the image formats then it would be very useful for us in daily scenario
In this article we will see how we can convert the Image Formats using Windows Application.

Objective

Converting Image Formats.



Using the code


  1. Create a New Windows Application Project in VS 2010.
  2. Drag and Drop 1 Picture Box, 2 Buttons,1 Open File Dialog and 1 Save File Dialog Box and 6 Radio buttons for specifying the conversion.
  3. The Form should look like Screen 1

Screen 1




Working of the Application

  1. First we will import the namespace using System.Drawing.Imaging; as the class in Imaging will give us the facility to change the formats of the Images.
  2. Then we will Load the Image by clicking the Load Button and then browse the image from out computer.
  3. We will then select any one of the formats from the options given in the radio button.
  4. Once we choose the format then we will convert button.As we click the convert button a save file dialog box will open up and ask us to save the image and ask us to give it a Name.
  5. Once we give the name and click OK the image will be converted and saved.
  6. You can then view the image and check its extension.

Screen 2 :- Loading the Image





Screen 3 :- Saving the Image




Screen 4 :- Conversion Successful




Screen 5 :- Output of Image



// Code behind
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace Image_Converter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // This will load the Image for conversion
        private void btnLoadImage_Click(object sender, EventArgs e)
        {
            ofdialog.Filter = "*.jpg|*.jpg|*.bmp|*.bmp|*.gif|*.gif|*.png|*.png";
            if (ofdialog.ShowDialog() == DialogResult.OK)
            {
                pbOgImage.Image = Image.FromFile(ofdialog.FileName);

            }
        }

        // This will set the extension while conversion
        private string AssignExtensionFilter()
        {
            if (rdJpeg.Checked) return "*.jpg|*.jpg";
            if (rdgif.Checked) return "*.gif|*.gif";
            if (rdpng.Checked) return "*.png|*.png";
            if (rdbmp.Checked) return "*.bmp|*.bmp";
            if (rdICO.Checked) return "*.ico|*.ico";
            if (rdExif.Checked) return "*.exif|*.exif";

            return string.Empty;
        }

        // The Imaging class will help to convert the image and save it as the desired format
        private void btnConvert_Click(object sender, EventArgs e)
        {
            Image convertedImage = pbOgImage.Image;
            sfdialog.Filter = AssignExtensionFilter();
            if (sfdialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                if (rdJpeg.Checked)
                {
                    convertedImage.Save(sfdialog.FileName, ImageFormat.Jpeg);
                    MessageBox.Show("Conversion Successfull");
                }
                else if (rdgif.Checked)
                {
                    convertedImage.Save(sfdialog.FileName, ImageFormat.Gif);
                    MessageBox.Show("Conversion Successfull");
                }
                else if (rdpng.Checked)
                {
                    convertedImage.Save(sfdialog.FileName, ImageFormat.Png);
                    MessageBox.Show("Conversion Successfull");
                }
                else if (rdbmp.Checked)
                {
                    convertedImage.Save(sfdialog.FileName, ImageFormat.Bmp);
                    MessageBox.Show("Conversion Successfull");
                }
                else if (rdICO.Checked)
                {
                    convertedImage.Save(sfdialog.FileName, ImageFormat.Icon);
                    MessageBox.Show("Conversion Successfull");
                }
                else if (rdExif.Checked)
                {
                    convertedImage.Save(sfdialog.FileName, ImageFormat.Exif);
                    MessageBox.Show("Conversion Successfull");
                }
                 
            }
            else
            {
                MessageBox.Show("Image is not Valid");
            }
        }

        
    }
}



Explanation of AssignExtensionFilter() Function

This function will set the extension on the base of the radio button selected by the user.



Conclusion


The main idea of writing this article is to give user a home made conversion app for everyday use.


Reference


Imaging concept understood from Microsoft MSDN Site

http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx

Page copy protected against web site content infringement by Copyscape

About the Author

Raj.Trivedi
Full Name: Raj Trivedi
Member Level:
Member Status: Member,MVP
Member Since: 6/16/2012 2:04:41 AM
Country: India
Regard's Raj.Trivedi "Sharing is Caring" Please mark as answer if your Query is resolved
http://www.dotnetfunda.com/profile/raj.trivedi.aspx
Raj Trivedi i.e. me started my career as Support Professional and then moved on the Software development eventually reached at these skills Software Development | Enthusiastic Blogger | Content Writer | Technical Writer | Problem Solver | Lecturer on Technology Subjects | Runnerup Award Winner on www.dotnetfunda.com and firm believer in Sharing as a way of Caring Yet this much achieved its still a long way to go and there is biggest dream lying to be one of the best entrepreneurs of India in Technology Department. The Dream has just started and i hope it follows. Highlights are mentioned in details in my profile at http://in.linkedin.com/pub/raj-trivedi/30/61/b30/

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)