Hi There
I am using .NET 2005 Beta 2, and am trying to write a utility program that converts icons to bitmaps.
The following function creates an icon file OK, but the icon file is not properly formed. When I try to open it using Visual Studio I just get a heap of jumbled characters.
public void BitmapToIcon(string sourceFileName, string destFileName)
{
// Create a Bitmap object from an image file.
Bitmap bmp = new Bitmap(sourceFileName);
// Get an Hicon for myBitmap.
IntPtr Hicon = bmp.GetHicon();
// Create a new icon from the handle.
Icon newIcon = Icon.FromHandle(Hicon);
//Write Icon to File Stream
FileStream fs = new FileStream(destFileName, FileMode.OpenOrCreate);
newIcon.Save(fs);
fs.Close();
}
TIA
Bill

C# 2.0 Convert Bitmap to Icon
DanRB
the bitmap file should be 32x32 pixels and no more than 256 colors.
note for
Arindam,
At least you get the hint to edit the file. I did not get that in c# vs 2008. it just reject the icon!
In severe case of invalid size and true color, VS crashed losing any unsaved change in the project!
Good thing I only have one number changed and recover easy enough. moral of story Save work before setting icon
melack
tax
Hi David
When I run the above code in 2003, I get an icon that I can open and edit in Visual Studio (both 2003 and 2005).
When I run the above code in VS 2005, it creates an icon file, but:
(1) When I try to view it in Windows Picture and Fax Viewer, it says "Preview Not Available"
(2) When I try to open it in Paint, it does not open, instead it gives an error "C:TEST.ico Paint Cannot Read this file. This is not a valid bitmap file, or its format is not currentlyt supported"
- When I try to open it in Visual Studio 2003/2005, it gives the same message as (3) ... so Visual Studio is trying to open it in paint for some reason
The above results occured on my work computer. On my home computer the same things happen in (1) and (2), but in (3) Visual Studio just opens a jumble of characters.
I'm really surprised that the same code works OK for you. Are you using one of the CTP versions of .NET 2 (available through MSDN subsciption)
For completeness, I also tried the following code in VS 2005:
Bitmap bmp = new Bitmap(sourceFileName);
bmp.Save(
@"C:\Test.ico",System.Drawing.Imaging.ImageFormat.Icon);This creates an icon file, but Visual Studio 2003/2005 will not open the icon file for editing. Instead, it opens the file in paint
I am so confused!!!
TIA
Bill
FrAgFo0d
After calling Icon.FromHandle, destroy the hIcon using:
[System.Runtime.InteropServices.DllImport("User32.dll")]
bool DestroyIcon(System::IntPtr hIcon);
Sathianantha Thilagar T
This is a note for people like me who don't know much about interop:
use
[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static bool DestroyIcon(IntPtr handle);
before the declaring the function creating the icon
and when done with creating the icon
will work fine as VS application icon as long as as the input bmp file is 256 color or less and size is 32x32 pixels
In summary, a working sample using the name space System.IO can be like this
private void buttonConvert2Ico_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "C:\\Data\\\" ;
openFileDialog1.Filter = "BitMap(*.bmp)|*.bmp" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
string sFn = openFileDialog1.FileName;
MessageBox.Show("Filename=" + sFn);
string destFileName = sFn.Substring(0, sFn.Length -3) +"ico";
// Create a Bitmap object from an image file.
Bitmap bmp = new Bitmap(sFn);
// Get an Hicon for myBitmap.
IntPtr Hicon = bmp.GetHicon();
// Create a new icon from the handle.
Icon newIcon = Icon.FromHandle(Hicon);
//Write Icon to File Stream
System.IO.FileStream fs = new System.IO.FileStream(destFileName, System.IO.FileMode.OpenOrCreate);
newIcon.Save(fs);
fs.Close();
DestroyIcon(Hicon);
//DestroyIcon( hIcon);
setStatus("Created icon From=" + sFn + ", into " + destFileName);
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read/write file. Original error: " + ex.Message);
}
}
}
BTW the above sample was tested in VS 2008 c# with .net 3.5. I have not tried prior version. I am getting to like vs 2008 express although I have not used much of .net 3.5 so far
BubbaRichard
Hi,
I'm also facing the problem.
private void ConvertToIcon(object sender, System.EventArgs e)
{
try
{
System.Drawing.Bitmap selectedBMP;
selectedBMP =
new Bitmap(this.mBMPFilePath);System.IntPtr iconHandle = selectedBMP.GetHicon();
System.Drawing.Icon icon = Icon.FromHandle(iconHandle);
this.Icon = icon; if(this.sveFlDlgIcon.ShowDialog().Equals(DialogResult.OK)){
this.mICOFilePath = this.sveFlDlgIcon.FileName;Stream opStream = File.Create(
this.mICOFilePath);icon.Save(opStream);
opStream.Flush();
opStream.Close();
}
}
catch(Exception ex){
MessageBox.Show(ex.Message,"Error saving Icon",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
this is the code snippet in the button click.
this.Icon = icon; this sets the Form icon perfectly.But if I save it as a file it's not opening in VS.NET.Also I can't referer that .ico file as the Icon of another Form in design mode giving an error
"The argument 'picture' must be a picture that can be used as a Icon."
Can some one put light on this
Regards,
Arindam
Martin Susil
What exactly is happening