Results 1 to 13 of 13

Thread: Create icon file with multiple icons in it

  1. #1

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Create icon file with multiple icons in it

    I've always had to use a program that does this stuff for me, but I'd like to just make my own program that does this. I've already got the individual bitmap images of the 3 sizes (16x16, 32x32 and 48x48) for the icon file and I usually find use some shareware program to create the icon file from the multiple bitmap images for me but I'm wondering if it's possible in .Net to create an icon file (*.ico) that contains multiple physical icons in it.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  2. #2
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Create icon file with multiple icons in it

    You can draw multiple images to another one, once you have done that just use this sub:

    Code:
    Private Sub CreateIcon(ByVal bitmapName As String)
    Try
    Dim fi As New System.IO.FileInfo(bitmapName)
    Dim bmp As New Bitmap(fi.FullName)
    Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(fi.FullName.Replace(fi.Extension, ".ico"))
    Icon.FromHandle(bmp.GetHicon).Save(sw.BaseStream)
    sw.Close()
    Catch ex As Exception
    System.Diagnostics.Debug.WriteLine(ex)
    End Try
    End Sub
    found it here : http://khsw.blogspot.com/2006/03/cre...ing-vbnet.html
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  3. #3

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Create icon file with multiple icons in it

    Quote Originally Posted by MonkOFox View Post
    You can draw multiple images to another one, once you have done that just use this sub:

    Code:
    Private Sub CreateIcon(ByVal bitmapName As String)
    Try
    Dim fi As New System.IO.FileInfo(bitmapName)
    Dim bmp As New Bitmap(fi.FullName)
    Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(fi.FullName.Replace(fi.Extension, ".ico"))
    Icon.FromHandle(bmp.GetHicon).Save(sw.BaseStream)
    sw.Close()
    Catch ex As Exception
    System.Diagnostics.Debug.WriteLine(ex)
    End Try
    End Sub
    found it here : http://khsw.blogspot.com/2006/03/cre...ing-vbnet.html
    Right, except that all 3 sizes will be showing at the same time for my exe. I need 3 images in the 1 file, so when windows explorer views my exe file in list view the 16x16 image is showing, but when it's set to icon/small view the 32x32 icon is showing (same exe) or when the exe file the 48x48 icon is what's showing from the icon file.
    Not all 3 icon sizes showing at the same time, only 1 of the 3 from inside is showing at any time, depending on Windows. It's an application icon.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Create icon file with multiple icons in it

    Interesting, I've always wondered how icons work in this way. I usually use a Photoshop plugin to create my high-quality (from transparent PNG images) icon files (compatible with Vista and up), but as far as I know it does not allow you to put multiple icons into a single file.

    I doubt that there is any built in functionality in the framework, so you will probably need to know exactly how the ICO filetype is built, or you need to find a third party implementation that will do it for you.

    I'll have a quick look, as this would be good to have.

    The Wikipedia entry covers it pretty well in case you end up writing your own implementation.

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Create icon file with multiple icons in it

    Hey JB,

    I started to work on this myself actually. I found an implementation in Java but I didn't look at it yet. For once I'm going to try to figure it out for myself just to see how far I can get.

    I figured reading ICO files would be the first step in understanding how they were build, so I implemented that first. I have not implemented reading the BMP data yet*, but so far it seems to get the type (1 for ICO, 2 for CUR files), the amount of images, and the sizes of the images.

    vb.net Code:
    1. Imports System.Drawing
    2. Imports System.IO
    3.  
    4. Public Class IcoFile
    5.  
    6.     Public Sub New(ByVal type As Integer, ByVal images As IEnumerable(Of Image))
    7.         If type <> 1 AndAlso type <> 2 Then Throw New ArgumentOutOfRangeException("type", "Argument 'type' must be 1 (ICO) or 2 (CUR).")
    8.         _Type = type
    9.         _Images = images
    10.     End Sub
    11.  
    12. #Region " Properties "
    13.  
    14.     Private _Type As Integer
    15.     Public ReadOnly Property Type() As Integer
    16.         Get
    17.             Return _Type
    18.         End Get
    19.     End Property
    20.  
    21.     Private _Images As IEnumerable(Of Image)
    22.     Public ReadOnly Property Images() As IEnumerable(Of Image)
    23.         Get
    24.             Return _Images
    25.         End Get
    26.     End Property
    27.  
    28.     Public ReadOnly Property ImageCount() As Integer
    29.         Get
    30.             If Me.Images Is Nothing Then Return 0
    31.             Return Me.Images.Count
    32.         End Get
    33.     End Property
    34.  
    35. #End Region
    36.  
    37.     Public Shared Function Read(ByVal path As String) As IcoFile
    38.  
    39.         Dim type As Integer
    40.         Dim imageCount As Integer
    41.         Dim images As New List(Of Image)
    42.  
    43.         Using input = File.OpenRead(path)
    44.             Using reader As New BinaryReader(input)
    45.  
    46.                 reader.ReadBytes(2)                 ' Reserved, should be 0
    47.                 type = reader.ReadInt16()           ' Icon type (1 for ICO, 2 for CUR)
    48.                 imageCount = reader.ReadInt16()     ' Number of images
    49.  
    50.                 For i As Integer = 0 To imageCount - 1
    51.                     reader.BaseStream.Position = 6 + 16 * i   ' 6 for the first 6 bytes, then 16 extra every iteration
    52.                     Dim width As Integer = reader.ReadByte()
    53.                     Dim height As Integer = reader.ReadByte()
    54.  
    55.                     If width = 0 Then width = 256 ' see wiki
    56.                     If height = 0 Then height = 256 ' see wiki
    57.  
    58.                     Dim bmp As New Bitmap(width, height)
    59.  
    60.                     ' TODO: Add bmp data
    61.  
    62.                     images.Add(bmp)
    63.                 Next
    64.  
    65.             End Using
    66.         End Using
    67.  
    68.         Return New IcoFile(type, images)
    69.     End Function
    70.  
    71. End Class

    Tested with
    vb.net Code:
    1. Using ofd As New OpenFileDialog
    2.             ofd.Filter = "ICO Files (*.ico)|*.ico|CUR Files (*.cur)|*.cur"
    3.             ofd.Multiselect = False
    4.  
    5.             If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
    6.  
    7.                 Dim ico As IcoLibrary.IcoFile = IcoLibrary.IcoFile.Read(ofd.FileName)
    8.  
    9.                 MessageBox.Show(ico.Type.ToString)
    10.                 For Each img As Image In ico.Images
    11.                     MessageBox.Show(img.Size.ToString())
    12.                 Next
    13.  
    14.             End If
    15.         End Using
    Tested it on my own icon which I knew to be 256x256 and returned that size. Then tested it on the icon of a game, which I was guessing would have all sizes, and it returned 256x256, 128x128, 48x48, 32x32, 24x24 and 16x16. It seems to work so far!


    *Reading the BMP data is probably more difficult, but so far it seems you just need to read the offset first (to figure out where it's stored) and then just read from there. I just don't really understand what that data would return. I don't think its the bytes for the image just yet as there is all kinds of bitwise maps (AND and XOR maps or something) and transparency and that stuff... Might try that tomorrow.


    As for writing, you can probably just do the same in reverse. Shouldn't be too hard once you understand which data goes where.

  6. #6

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Create icon file with multiple icons in it

    So I've been gone for a while here, Nick how far were you able to get with this? I'd like to pick up where you've left off.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Create icon file with multiple icons in it

    Not very far. I can read icons just fine, using my own code, or some code I found on the web, or using APIs from Chris' API pack. But there doesn't seem to be a way to write icons. I can't figure out how to write the data on my own, and the sources online I find all don't implement any save functions. Most of them do have them, but they either throw a NotImplementedException, or there's just no code in them. In one source, there was code to save, but there was no way to provide any list of icons... So still pretty useless :\

    So in short, I can read just fine, but I cannot write them.

  8. #8

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Create icon file with multiple icons in it

    When you get the chance, send me the source files.

    If you're reading on the bit level, can't we just write on the bit level too?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  9. #9
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Create icon file with multiple icons in it

    Here's the best one of the few I have. It's not my own code, I translated it from here and renamed some things. It worked on all icons I threw at it so far, including transparent ones (which my own code had problems with).

    Basically, the IcoFile class is what represents one ICO file, containing a bunch of IconDeviceImages in an IconDeviceImageCollection. The IconDeviceImage class represents a single image in the icon.

    The IcoFile class has a Save method which should save it back to the specified path, but I haven't tried it yet because I don't know how to actually change the ICO file (eg: change or add new images). Probably you'd have manually add a new instance of the IconDeviceImage class, but I've no idea how to determine how to set its properties... There is a constructor that accepts an Icon, but there's no code in there so that doesn't do anything...

    That's basically where I left it.
    Attached Files Attached Files

  10. #10
    Addicted Member
    Join Date
    Mar 2008
    Posts
    143

    Re: Create icon file with multiple icons in it

    Shut up and eat your banana!

  11. #11

  12. #12

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Create icon file with multiple icons in it

    Quote Originally Posted by NickThissen View Post
    Haven't looked at the source yet but it seems again read-only, and no saving implemented... Correct me if I'm wrong
    I haven't fiddled with much here, but it looks like in that link it'd be just a matter of adding code for more iconEntity's to be added to the collection, update the header count and as for actually writing a file, create a file stream and write the bytes back out starting with the header info then loop the collection and write that info out.

    I've taken a glance at your code there Nick and I gotta say that I'm not quite able to follow all of that, is there a specific class that I use or am I missing something? There's a lot of api calls too
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  13. #13
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Create icon file with multiple icons in it

    The IcoFile class is the one you need, the rest is just used internally. The IconDeviceImage class represents one image (including header information, and some stuff to write/read it), and the IcoFile class has a collection of those so that represents one ICO file.

    As for writing; yeah you'd just need to write the bytes in the correct order, but the problem is that I don't know what values to use, nor how to convert an actual image to a byte stream in the correct format. From my reading attempts, you cannot simply use Image.FromStream or something like that as the bytes are not in the usual image format (they contain their own header and stuff which you need to write out too).

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