PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Create Valid Icon Files! In 24-bit true color!-VBForums
Results 1 to 4 of 4

Thread: Create Valid Icon Files! In 24-bit true color!

  1. #1

    Thread Starter
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation

    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:
    1. Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
    2.         If dlgOpen.ShowDialog() = DialogResult.OK Then
    3.             'assumes, for sample, that bitmap is already at 32 px by 32 px
    4.             Dim MyBMP As New Bitmap(dlgOpen.FileName)
    5.             PictureBox1.Image = MyBMP
    6.         End If
    7. End Sub
    9. Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
    10.         If dlgSave.ShowDialog = DialogResult.OK Then
    11.             'creates icon file so we can edit it with IconEX.dll
    12.             Dim MyBMP As New Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
    13.             Dim MyIcon As Icon = Icon.FromHandle(MyBMP.GetHicon)
    14.             Dim st As System.IO.Stream = New System.IO.FileStream _
    15.                               (dlgSave.FileName, IO.FileMode.Create)
    16.             Dim wr As New System.IO.BinaryWriter(st)
    17.             MyIcon.Save(st)
    18.             MyBMP.Dispose()
    19.             wr.Close()
    20.             '-- END icon creation --
    22.             'Opens icon file we just created for editing with IconEX
    23.             Dim Iconex As New vbAccelerator.Components.Win32.IconEx(dlgSave.FileName)
    24.             'Removes original icon image that we created above
    25.             Iconex.Items.RemoveAt(0)
    26.             'Creates a new 32 x 32 IconDeviceImage, to store the new icon image
    27.             Dim IconDeviceImage As New vbAccelerator.Components.Win32.IconDeviceImage _
    28.                                              (New Size(32, 32), ColorDepth.Depth32Bit)
    29.             'gets bitmap of (assumed) 32 x 32 image in picturebox, sets it to IconImage
    30.             IconDeviceImage.IconImage = New Bitmap(PictureBox1.Image)
    31.             'adds icondevicimage to the icon file
    32.             Iconex.Items.Add(IconDeviceImage)
    33.             'saves icon
    34.             Iconex.Save(dlgSave.FileName)
    35.             MessageBox.Show("Icon Created!")
    36.         End If
    37. 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...
    Attached Files Attached Files
    Last edited by gigemboy; Apr 5th, 2006 at 11:04 AM.

  2. #2
    New Member
    Join Date
    Jul 2006

    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!

  3. #3
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Brisbane, Australia

    Thumbs up Re: Create Valid Icon Files! In 24-bit true color!

    most things dont work straight out of the box for me

  4. #4
    New Member
    Join Date
    Oct 2011

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width