Results 1 to 7 of 7

Thread: Converting Bitmap to Icon

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    Converting Bitmap to Icon

    Hey. In my program I have a picture box that has a picture in it. Is there a way that I can have my program convert that to an icon file and shrunk down so it can be used in the system tray? Any ideas?

    Thanks
    John

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Converting Bitmap to Icon

    VB Code:
    1. Dim myBitmap As Bitmap = New Bitmap(16, 16) '16x16 pixels bitmap
    2.         Dim Gr As Graphics = Graphics.FromImage(myBitmap) 'Graphics object from bitmap
    3.  
    4.         'Draw Pictureimage to bitmap scaled to 16x16 pixels
    5.         Gr.DrawImage(PictureBox1.Image, New Rectangle(0, 0, 16, 16), _
    6.                      PictureBox1.ClientRectangle, _
    7.                      GraphicsUnit.Pixel)
    8.  
    9.         'Create Icon from bitmap
    10.         Dim Hicon As IntPtr = myBitmap.GetHicon()
    11.         Dim myNewIcon As Icon = Icon.FromHandle(Hicon)
    12.  
    13.         'Save icon to file
    14.         Dim fs As System.IO.Stream = IO.File.OpenWrite("C:\MyIcon5.ico")
    15.         myNewIcon.Save(fs)
    16.  
    17.         Gr.Dispose() 'Dispose graphics
    18.         fs.Close()   'Dispose Stream
    Saving to file is optional, you could directly add myNewIcon to Systray.
    The icon created won't have more than 16 colors.
    Last edited by jcis; Apr 1st, 2006 at 12:46 AM.

  3. #3
    Member
    Join Date
    Mar 2006
    Posts
    60

    Re: Converting Bitmap to Icon

    Just while on the subject of icons, I created a file in paint and named it as "myname.ico"
    when I click on my main form and try to add this icon it tells me that the "picture" must be a picture that can be used as an icon.

    any suggestions?

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Converting Bitmap to Icon

    You can't create icons with MSPaint, that you created is just an image file. You can view and create icons with Visual Studio.Net

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    Re: Converting Bitmap to Icon

    hey thanks a lot. Is there any way that I can have VB make the image have a transparent background. Like likes say my picture is a car with a white background. I know this complex because I can do it in photoshop and it doesn't always turn out right. Any ideas?

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Converting Bitmap to Icon

    Ahhh the great icon question I posted a thread about this here:
    http://www.vbforums.com/showthread.php?t=395933

    The method jcis provided works, but the icon only has 16 colors (4 bits per pixel) (see thread link above). There is no way natively to do it in .NET, because .NET doesnt have an Icon encoder. The Icon.Save method does not work, and there is a reference in the above thread link to a Microsoft KB article that explains why. Doesnt seem to make sense to put a Save method if the file you save doesnt work hehe.

    I have found a hack that allows you to save 24 bit color icons, as well as multiple icon sizes inside of the icon file, as well as transparency. It uses the IconEX class posted here in C#:http://www.vbaccelerator.com/home/NE...onExplorer.asp

    The problem was it required an existing icon file present, so the "hack" is to use the above method that jcis posted to create a blank icon file, then open the icon using the methods in the IconEX class, remove the icon images in the file, then add the new icon images you want, then re-save. This has been tested to work, and I think I'm going to post this hack in the codebank forum in another couple days or so once I get my program finished.

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Converting Bitmap to Icon

    Posted a new codebank example that shows how to create valid icon files from bitmaps...

    http://www.vbforums.com/showthread.php?t=396650

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