Convert any Image to Icon in C#

naimish's Avatar author of Convert any Image to Icon in C#
This is an article on Convert any Image to Icon in C# in ASP.NET.
Rated 5.00 By 1 users

Introduction



Usually in Projects we need some images which are in icon format. Generally we get images in .JPG OR .BMP format. This code snippet when compiled and executed on .Net machine will convert the JP OR BMP file in to .ico format.

The code



Code: .NET

Code:
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;
namespace iconConvertor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnSelectSrcImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog browseFile = new OpenFileDialog();
            browseFile.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
            browseFile.Title = "Select Image file";
            if (browseFile.ShowDialog() == DialogResult.Cancel)
                return;
            try
            {
                sourceImage.Text = browseFile.FileName;
            }
            catch (Exception)
            {
                MessageBox.Show("Error opening file", "File Error",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        private void btnConvert_Click(object sender, EventArgs e)
        {
            if (sourceImage.Text == "")
                MessageBox.Show("Please select an Image file");
            Bitmap Cbitmap=null;
            try
            {
                Cbitmap = new Bitmap(sourceImage.Text); //(32, 32, PixelFormat.Format64bppPArgb);
            }
            catch(Exception)
            {
                MessageBox.Show("Error opening given Image file", "File Error",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            Cbitmap.MakeTransparent(Color.White);
            System.IntPtr icH = Cbitmap.GetHicon();
            Icon ico = Icon.FromHandle(icH);
            Cbitmap.Dispose();
            System.IO.FileStream f = new System.IO.FileStream(destinationFldr.Text + "\\image.ico", System.IO.FileMode.OpenOrCreate);
            ico.Save(f);
            MessageBox.Show("Image is converted to ICON and saved in the directory: " + destinationFldr.Text + " with name image.ico"); 
        }
        private void btnSelectDestinationFldr_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog browseFolder = new FolderBrowserDialog();
            //browseFolder.Filter = "Directory (/AD)";

            if (browseFolder.ShowDialog() == DialogResult.Cancel)
                return;
            try
            {
                destinationFldr.Text = browseFolder.SelectedPath;
            }
            catch (Exception)
            {
                MessageBox.Show("Error opening file", "File Error",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        private void sourceImage_TextChanged(object sender, EventArgs e)
        {
        }
    }
}
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nomination for Article of the month - Sep 2009 Started. Nominate this article.
0
almedajohnson's Avatar, Join Date: Sep 2010
Newbie Member
Quote:
Originally Posted by shabbir View Post
Nomination for Article of the month - Sep 2009 Started. Nominate this article.
Hey
What is this article of the month?? Is it any kind of competition here on forum? I would like to participate in this competition if it is still on so give me details
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by almedajohnson View Post
Hey
What is this article of the month?? Is it any kind of competition here on forum? I would like to participate in this competition if it is still on so give me details
Yes we do have a competition every month and check out Monthly Article Competition Forum

Don't forget to check out the Rules and Regulations.