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
Printable View
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
Saving to file is optional, you could directly add myNewIcon to Systray.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
The icon created won't have more than 16 colors.
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?
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
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?
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.
Posted a new codebank example that shows how to create valid icon files from bitmaps...
http://www.vbforums.com/showthread.php?t=396650