Saving an image as an .ico file

J

Jason Barnett

When trying to save an image as an icon, I've used the following line of
code (img is an System.Drawing.Image)...

img.Save("C:\output.ico", ImageFormat.Icon)

The files saves without an error, and it can be opened by Windows Picture
and Fax Viewer, but I cannot assign the image as a form icon. Does anyone
know what I can do to get this to work?
 
M

Michel van den Berg

Dear Jason Barnett,

Why aren't you able to assign the image as a form icon? Does it produce an
error, or aren't you able to browse to it? Could you specify your problem a
bit more?

Michel van den Berg
 
J

Jason Barnett

I'm able to save the image with an .ico extension, however I don't think
it's in the correct .ico format. When I attempt to load it as an Icon
object, I receive an ArgumentException (with the message, "Argument
'picture' must be a picture that can be used as a Icon."). I'm able to
browse to the file and open it with "Windows Picture and Fax Viewer", but
the image does not appear as the icon for the file when I look at it in
Windows Explorer.

Here's a more detailed example of my code:

Dim icoFile As String = "C:\output.ico"

Dim img As Image = Image.FromFile("C:\doc01.gif")
img.Save(icoFile, ImageFormat.Icon)

Dim icon As New Icon(icoFile)
Me.Icon = icon
 
M

Michel van den Berg

Dear Jason Barnett,

Try this:

Dim outputFileName As String = "C:\output.ico"
Dim inputFileName As String = "C:\input.gif"
Dim bmp as Bitmap = Ctype(Image.FromFile(inputFileName, Bitmap)
Dim ico as Icon = Icon.FromHandle(bmp.GetHicon())
Dim file as FileStream = new FileStream(outputFileName,
FileMode.OpenOrCreate)
ico.Save(file)
file.Close()
ico.Dispose();

Please note, that this is untested code, so I don't know if it will work.

Michel van den Berg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top