none
convert image to icon

    Question

  • hi all

    i have a project that can convert image to icon

    but when i save it deos not appear to me that it is having the right format of the icon

    and here is some code in my form

     

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

    thanks in advance
    Sunday, August 14, 2011 6:33 AM

Answers

  • Your code is fine. It converts a imageto an Icon.

    What's the dimensions of your original JPG? If they aren't one of the standard icon sizes, you'll get a strange behavior. If the original JPG is too large, you'll get the error you are seeing.

    Some of the more common sizes are 16x16, 24x24, 32x32, 48x48.


    My Blogs

    Sunday, August 14, 2011 11:59 AM

All replies

  • Your code is fine. It converts a imageto an Icon.

    What's the dimensions of your original JPG? If they aren't one of the standard icon sizes, you'll get a strange behavior. If the original JPG is too large, you'll get the error you are seeing.

    Some of the more common sizes are 16x16, 24x24, 32x32, 48x48.


    My Blogs

    Sunday, August 14, 2011 11:59 AM
  • hi Zain_Ali

    thnx very much :D

    Sunday, August 14, 2011 12:20 PM
  • Welcome to back if you have any question later.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 12, 2011 9:37 AM