2 Attachment(s)
Create Valid Icon Files! In 24-bit true color!
Icon files created in .NET do not work. The Image.Save method or Icon.Save method allows you to choose an icon filetype, but it appears that you cant use the file that it created for anything, as it is not a valid icon file. There are ways around this, with the method stated in this thread. The problem with it is that it only saves 16 color icons. The reason for this is because .NET does not have an encoder for Icon files. You can only read icon files in and retrieve the images, not write the icon file back. Proof of this can be found in this knowledgebase article. So I was determined to search out a way in order to get valid Icon files for .NET. That led me to IconEX.
IconEX is a class found in .NET Icon Explorer, by its creator, Steve McMahon. It was written in C#, and instead of having to convert it, I just built the class into a dll, which can be found in the attached project sample at the bottom of the post.
The methodology is pretty simple, once you get past the first hurdle. IconEX was built to read the contents of an existing icon file, and write them back. In order to use it, you need to have an existing icon file. The way I solved this was to create an icon file using the method linked in the forum thread above, which only creates a 16 color icon file. Then you simply open this icon file with the methods in the IconEX class, remove the image it contains, and add the new images you want into it. A general example is below. It is important to note that in this example, it assumes that you opened a 32 x 32 pixel bitmap.
Sample Project Code:
VB Code:
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
If dlgOpen.ShowDialog() = DialogResult.OK Then
'assumes, for sample, that bitmap is already at 32 px by 32 px
Dim MyBMP As New Bitmap(dlgOpen.FileName)
PictureBox1.Image = MyBMP
End If
End Sub
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
If dlgSave.ShowDialog = DialogResult.OK Then
'creates icon file so we can edit it with IconEX.dll
Dim MyBMP As New Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim MyIcon As Icon = Icon.FromHandle(MyBMP.GetHicon)
Dim st As System.IO.Stream = New System.IO.FileStream _
(dlgSave.FileName, IO.FileMode.Create)
Dim wr As New System.IO.BinaryWriter(st)
MyIcon.Save(st)
MyBMP.Dispose()
wr.Close()
'-- END icon creation --
'Opens icon file we just created for editing with IconEX
Dim Iconex As New vbAccelerator.Components.Win32.IconEx(dlgSave.FileName)
'Removes original icon image that we created above
Iconex.Items.RemoveAt(0)
'Creates a new 32 x 32 IconDeviceImage, to store the new icon image
Dim IconDeviceImage As New vbAccelerator.Components.Win32.IconDeviceImage _
(New Size(32, 32), ColorDepth.Depth32Bit)
'gets bitmap of (assumed) 32 x 32 image in picturebox, sets it to IconImage
IconDeviceImage.IconImage = New Bitmap(PictureBox1.Image)
'adds icondevicimage to the icon file
Iconex.Items.Add(IconDeviceImage)
'saves icon
Iconex.Save(dlgSave.FileName)
MessageBox.Show("Icon Created!")
End If
End Sub
You should now have a 32 x 32 pixel icon with 24 bit color :) The class also supports a mask image, and transparency, but not included in this sample code. You can also include multiple icon sizes in the resulting icon file by simply adding a new IconDeviceImage with the new size using the IconEx.Items.Add method.
The EZIconConverter program in my signature was created using the IconEX class, and supports transparency and automatic image resizing.
**Note - Dll and project was compiled using .NET 2003 for the 1.1 framework, so no guarantees that it will work under other versions of .NET.
**Note2 - You may need to remove and re-add the reference to IconEX.dll in order for the sample project to work, though not sure...
Re: Create Valid Icon Files! In 24-bit true color!
This works fantastic! Ive looked everywhere for a good example of how to create icons with VB and none of them worked.
Great Job! :thumb:
Re: Create Valid Icon Files! In 24-bit true color!
most things dont work straight out of the box for me
excellent
Re: Create Valid Icon Files! In 24-bit true color!
Gigemboy, great post! Do you have the link to the EZIconConverter program that supports masks and transparency?