|
-
Mar 31st, 2006, 11:58 PM
#1
Thread Starter
Addicted Member
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
-
Apr 1st, 2006, 12:24 AM
#2
Re: Converting Bitmap to Icon
VB Code:
Dim myBitmap As Bitmap = New Bitmap(16, 16) '16x16 pixels bitmap
Dim Gr As Graphics = Graphics.FromImage(myBitmap) 'Graphics object from bitmap
'Draw Pictureimage to bitmap scaled to 16x16 pixels
Gr.DrawImage(PictureBox1.Image, New Rectangle(0, 0, 16, 16), _
PictureBox1.ClientRectangle, _
GraphicsUnit.Pixel)
'Create Icon from bitmap
Dim Hicon As IntPtr = myBitmap.GetHicon()
Dim myNewIcon As Icon = Icon.FromHandle(Hicon)
'Save icon to file
Dim fs As System.IO.Stream = IO.File.OpenWrite("C:\MyIcon5.ico")
myNewIcon.Save(fs)
Gr.Dispose() 'Dispose graphics
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.
-
Apr 1st, 2006, 04:17 AM
#3
Member
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?
-
Apr 1st, 2006, 04:49 AM
#4
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
-
Apr 1st, 2006, 10:42 AM
#5
Thread Starter
Addicted Member
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?
-
Apr 1st, 2006, 02:44 PM
#6
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.
-
Apr 1st, 2006, 09:15 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|