Cameron Kloot

hi,
what is the best way to create an icon file
thanks
Cameron



Re: Visual C# Express Edition creating an icon

ahmedilyas

can you explain more in detail on what you mean by your question

Are you talking about creating an icon file in your application

Are you talking about creating an icon FOR your application and using VS.NET to create the icon






Re: Visual C# Express Edition creating an icon

Cameron Kloot

to create an icon file, not an icon for my app. during runtime loading a pic and saving it as an icon





Re: Visual C# Express Edition creating an icon

ahmedilyas

sure, in that case it is not too difficult.

once you have a image object, which contains the picture, simply call the Save method giving it the format to save to...example:

Dim myImage as Image = Image.FromFile("myFilename.jpg")

myImage.Save("filename.ico", System.Drawing.Imaging.ImageFormat.Icon)

and that should save the bitmap image into an icon file.

I think in order for it to become an "true" icon, the image needs to be resized to 16*16 pixels

Does this help






Re: Visual C# Express Edition creating an icon

Cameron Kloot

well, i have tried it like that before but not with changing the size to 16,16. would that do it also ur code is in vb Wink





Re: Visual C# Express Edition creating an icon

ahmedilyas

my apologies! This is what happens when you know 2 languages very well but get confused which forum you are responding to ;-)

C# code:

Image myImage = Image.FromFile("myFilename.jpg");

myImage.Save("filename.ico", System.Drawing.Imaging.ImageFormat.Icon);

yes, I believe you need to resize the image to 16*16 format as this is what the Icon requires in Windows. So to resize:

Image img = Image.FromFile("filename.jpg");

using (Bitmap bmp = new Bitmap(16, 16))

{

using (Graphics g = Graphics.FromImage((Image)bmp))

{

g.DrawImage(img, 0, 0, bmp.Width, bmp.Height);

g.Save();

}

bmp.Save("icon.ico", System.Drawing.Imaging.ImageFormat.Icon);

}

does this help






Re: Visual C# Express Edition creating an icon

Cameron Kloot

well, it does the exact same thing as before:
it creates a new Icon.Document file that isnt recognized as a file, but still can be opened in pain.
and it doesnt work when i try to load it as an icon in visual studio. "Argument 'picture' must be a picture that can be used as an icon"






Re: Visual C# Express Edition creating an icon

ahmedilyas

yes ive just spotted that. Apperently it's a known issue....

http://channel9.msdn.com/ShowPost.aspx PostID=133634

I dont know what to suggest yet but should I, or any one else, find a solution - will be find to post. If you find one, please do post.






Re: Visual C# Express Edition creating an icon

Cameron Kloot

wah ;(

edit:
so there is no workaround






Re: Visual C# Express Edition creating an icon

jrboddie

The Chanel 9 article that ahmedilyas referenced mentioned a free .dll called FreeImage. I gave it a try and with the help of a wrapper found in CodeProject was able to convert a .bmp file to a .ico file.

Here are the steps:

1. Download the FreeImage .dll from here.

2. Copy the .dll into the bin/Debug and/or bin/Release folder in your project

3. Add an Enum and FreeImage Class to your project (shown in the example below).

4. Use three calls to the functions in FreeImage: load the bitmap file, save in icon format, release the resource.

The following example loads a .bmp into FreeImage and saves it back as a .ico file. It includes the required Enum and FreeImage Class. (Requires using System.Runtime.InteropServices; )

class Program

{

static void Main(string[] args)

{

int handle = FreeImage.FreeImage_Load(

FIF.FIF_BMP,

@"C:\Documents and Settings\User\Desktop\myBmp.bmp",

0);

FreeImage.FreeImage_Save(

FIF.FIF_ICO,

handle,

@"C:\Documents and Settings\User\Desktop\new.ico",

0);

FreeImage.FreeImage_Unload(handle);

}

}

public enum FIF

{

FIF_UNKNOWN = -1,

FIF_BMP = 0,

FIF_ICO = 1,

FIF_JPEG = 2,

FIF_JNG = 3,

FIF_KOALA = 4,

FIF_LBM = 5,

FIF_MNG = 6,

FIF_PBM = 7,

FIF_PBMRAW = 8,

FIF_PCD = 9,

FIF_PCX = 10,

FIF_PGM = 11,

FIF_PGMRAW = 12,

FIF_PNG = 13,

FIF_PPM = 14,

FIF_PPMRAW = 15,

FIF_RAS = 16,

FIF_TARGA = 17,

FIF_TIFF = 18,

FIF_WBMP = 19,

FIF_PSD = 20,

FIF_CUT = 21,

FIF_IFF = FIF_LBM,

FIF_XBM = 22,

FIF_XPM = 23

}

public class FreeImage

{

[DllImport("FreeImage.dll")]

public static extern int FreeImage_Load(FIF format,

string filename, int flags);

[DllImport("FreeImage.dll")]

public static extern void FreeImage_Unload(int handle);

[DllImport("FreeImage.dll")]

public static extern bool FreeImage_Save(FIF format,

int handle, string filename, int flags);

}

I believe there is a maximum size of 128x128 and I only tried to convert a .bmp although other formats may be possible.





Re: Visual C# Express Edition creating an icon

nobugz

Yup, it's a dirty little secret of GDI+, one of many. It doesn't actually support a bunch of the ImageFormats that Image.Save() accepts. It used to throw an exception back in the .NET 1.1 days. Too many people complained about that so they fixed it. In .NET 2.0, any of the ImageFormat values that are not supported (Emf, Icon, Wmf) makes it save the image in the PNG format. Gack.

Your only alternative is Bitmap.GetHicon(), it promises to convert a bitmap into an icon handle which you can then use with Icon.FromHandle(). Don't get your hopes up, it does an absolutely horrible job of mapping the colors in the original bitmap to colors in the icon. Unless you use only primary colors, you are not going to be pleased with the result.

There's only one true fix for this: use an icon editor to create a pretty one. I use Microangelo, recommended. The Visual Studio IDE has one too. And don't forget to steal, beg and borrow. Making pretty icons is hard for a left-thinking geek like me. Then again, you strike me more as a right-thinker.





Re: Visual C# Express Edition creating an icon

Cameron Kloot

well, i need to make the icons during runtime, usially just from random bmps, pngs, etc. i will try the GetHIcon() and FromHandle(). how do the icon creators make good looking icons what do u mean left and right thinking Wink
thanks





Re: Visual C# Express Edition creating an icon

Cameron Kloot

how would use the icon.Save() method i have tried it like this:
SaveFileDialog save = new SaveFileDialog();
if (save.ShowDialog() == DialogResult.OK)
{
Bitmap b = new Bitmap(pictureBox1.Image, new Size(16, 16));
Icon i = Icon.FromHandle(b.GetHicon());
Stream s = File.Create(save.FileName);
i.Save(i);
but it doesnt work ;(







Re: Visual C# Express Edition creating an icon

ahmedilyas

What doesnt work What happens

The Icon's save method accepts a stream as a parameter, you are currently giving it the icon to save...it should be the stream object, declared as s:

i.Save(s);

What happens






Re: Visual C# Express Edition creating an icon

Cameron Kloot

well, it runs and creates the file, but the file is of the type "File" not icon.