locked
convert picture to icon RRS feed

  • Question

  • hello everyone. iv been try to figure out how to use this code for a while. if someone could help that would be great.
    i think my problem is it dosent get the file to convert to a icon but i don't know what other code to use.
    Imports System.IO
    Imports System.Drawing.Imaging
    Public Class Form1
        Dim fi As IO.FileInfo
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            ' get directory
            FolderBrowserDialog1.ShowDialog()
            ListBox1.Items.Clear()
            Dim dir_info As New DirectoryInfo(FolderBrowserDialog1.SelectedPath)
    
            ' set file types
            Dim pattern(4) As String
            pattern(0) = "*.png"
            pattern(1) = "*.bmp"
            pattern(2) = "*.jpg"
            pattern(3) = "*.jpeg"
            pattern(4) = "*gif"
    
            ' get file
            For i As Int16 = 0 To 4
                ListFiles(ListBox1, pattern(i), dir_info)
            Next
        End Sub
    
        'Convert bitmap to icon
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            Dim i As Integer
            Dim file, name As String
            Dim length As Integer
    
            Try
                For i = 0 To (ListBox1.Items.Count - 1)
                    file = ListBox1.Items.Item(i)
                    fi = New System.IO.FileInfo(file)
                    length = file.Length - file.LastIndexOf(".")
                    name = file.Replace(file.Substring(file.LastIndexOf("."), +length), ".ico")
                    'fi = New System.IO.FileInfo(name)
                    Icon.Save(fi.Create)
    
                    '---'bmp.Save(name, ImageFormat.Icon)
    
                    name = ""
    
                Next
            Catch ex As Exception
                Show(ex)
            End Try
        End Sub
    
        'Put file in directory in listbox
        Private Sub ListFiles(ByVal lst As ListBox, ByVal pattern As String, ByVal dir_info As DirectoryInfo)
    
            ' get files in directory
            Dim fs_infos() As FileInfo = dir_info.GetFiles(pattern)
    
            ' add files to listbox
            For Each file1 As FileInfo In fs_infos
                ListBox1.Items.Add(file1.FullName)
            Next file1
    
            fs_infos = Nothing
    
            ' Search subdirectories.
            Dim subdirs() As DirectoryInfo = _
                dir_info.GetDirectories()
            For Each subdir As DirectoryInfo In subdirs
                ListFiles(lst, pattern, subdir)
            Next subdir
        End Sub
    
        Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
    
           
            PictureBox1.ImageLocation = ListBox1.SelectedItem
    
        End Sub
    
    End Class
    

    if anyone could help or point me in the direction to go that would be great. thank you
    Saturday, January 2, 2010 7:53 AM

Answers

  • You aren't loading the image from the selected file before saving it. To load the image and save it as .ICO, use:

    Image.FromFile(File).Save(Name, ImageFormat.Icon)

     

     


    • Proposed as answer by konikula Sunday, January 3, 2010 2:00 AM
    • Marked as answer by Youssif9 Sunday, January 3, 2010 5:27 AM
    Saturday, January 2, 2010 8:40 AM
  • The browser you are using to view files in the folder will have a processor for icon files.  But simply naming a file as *.ICO and saving it as type ICON does not create a correct icon file.  The browser is telling you that this icon file is not in the correct format for a genuine icon. Your image viewer, on the other hand, is able to see that the file is actually a bitmap, and will display the image correctly.

    An icon file will contain multiple images at different sizes, bit depths and image types.  It will also contain information about transparency for indexed image types.  For instance, for Vista it will probably have a 256x256 PNG image (which might be compressed), a 32x32 8-bit image, a 16x16 8-bit image and possibly additional monochrome images. To create a complete icon file you need to create each of these images, and then combine them into the correct icon file format.

    There's an example application here, which will give you an idea of the procedures involved:
    http://www.axialis.com/download/

    I don't know if there any examples available in VB for doing this, but it could be worth a search.

    • Marked as answer by Youssif9 Sunday, January 3, 2010 5:27 AM
    Sunday, January 3, 2010 2:29 AM

All replies

  • You aren't loading the image from the selected file before saving it. To load the image and save it as .ICO, use:

    Image.FromFile(File).Save(Name, ImageFormat.Icon)

     

     


    • Proposed as answer by konikula Sunday, January 3, 2010 2:00 AM
    • Marked as answer by Youssif9 Sunday, January 3, 2010 5:27 AM
    Saturday, January 2, 2010 8:40 AM
  • thanks that worked. but why when i look at the icon in the folder the icon seems blank. 
    i have to open the file to see the icon.


    you can see here

    Image and video hosting by TinyPic
    Sunday, January 3, 2010 2:06 AM
  • The browser you are using to view files in the folder will have a processor for icon files.  But simply naming a file as *.ICO and saving it as type ICON does not create a correct icon file.  The browser is telling you that this icon file is not in the correct format for a genuine icon. Your image viewer, on the other hand, is able to see that the file is actually a bitmap, and will display the image correctly.

    An icon file will contain multiple images at different sizes, bit depths and image types.  It will also contain information about transparency for indexed image types.  For instance, for Vista it will probably have a 256x256 PNG image (which might be compressed), a 32x32 8-bit image, a 16x16 8-bit image and possibly additional monochrome images. To create a complete icon file you need to create each of these images, and then combine them into the correct icon file format.

    There's an example application here, which will give you an idea of the procedures involved:
    http://www.axialis.com/download/

    I don't know if there any examples available in VB for doing this, but it could be worth a search.

    • Marked as answer by Youssif9 Sunday, January 3, 2010 5:27 AM
    Sunday, January 3, 2010 2:29 AM
  • thank you that explains a lot. sometimes there is just somethings Google wont tell you.

    you answer give me a perfect explanation on what happens.
    Sunday, January 3, 2010 5:30 AM