How to convert a Bitmap to Icon at code level?


Author
Message
Ben Hayat
Ben Hayat
Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)
Group: Forum Members
Posts: 374, Visits: 1.2K
Here is where I'm running to this issue.



I have a "NotifyIcon" that at runtime, I need to change the icon. But I don't want to grab the icon from disk. So, I placed an "ImageList" and put my Icons in it.



Now when I try to assign one of those images to NotifyIcon.Icon, the compiler complains that the Image from Imagelist is not an "Icon".

notifyIcon1.Icon = imageList1.Images[0];



I've looked around and can't find a way to convert the image from Imagelist to Icon.



Any pointers?

Thanks!

..ßen
StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Hehe, no not really... the Image and Icon are completely different... mainly because Icon does not inherit from Image (the file formats are completely different since an Icon can have several different sizes within the file).  So, I would recommend that you add the icons to your project (not as resources, just add them to the project with the "Add Exisiting Item" option) and change their compile option to Embedded Resource (in the properties for the file).  Then, you can use the Icon constructor that accepts a stream to retrieve the embedded resources.  Kinda like this:

notifyIcon1.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("RootNamespace.Folders.FileName"));

C# and VB do some different stuff for the name of the resource when it's compiled.  C# uses the root namespace + the folder names separated by "." followed by the filename.  Whereas VB just the root namespace + the filename.  If you ever want to see what resources are available, you can use the Assembly.GetExecutingAssembly().GetManifestResourceNames(); method which will return the names of all of the resources.  Oh, and the names are case-sensitive. 

But that's probably your best be for retrieving icons from the assembly at runtime.

Ben Hayat
Ben Hayat
Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)
Group: Forum Members
Posts: 374, Visits: 1.2K
Thank you very much Sir;



Hehe, no not really... the Image and Icon are completely different... mainly because Icon does not inherit from Image (the file formats are completely different since an Icon can have several different sizes within the file).
I sadly learned that very quickly (not that I was happy about)



So, I would recommend that you add the icons to your project (not as resources, just add them to the project with the "Add Exisiting Item" option) and change their compile option to Embedded Resource (in the properties for the file).
This the way I thought about doing, but didn't know I can "Embed" a resource. I need to look at that.



C# and VB do some different stuff for the name of the resource when it's compiled.
I remember clearly from the class you brought that point up, about the differences in VB and C# and also with the "With" clause!



I'm going to approach it this way. Thanks again.



p.s. Ben if you don't mind, I'm going to put your solution in the new section of samples as a new library of codes. If you don't like it, go ahead and delete it.

..ßen
Paul Chase
Paul Chase
Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)
Group: Forum Members
Posts: 414, Visits: 2.8K
Ben H,

Here is how to accomplish what you originally asked in VB, For C# you would probably just have to change the casting.

 NotifyIcon1.Icon = System.Drawing.Icon.FromHandle(CType(Me.ImageList1.Images(0), Drawing.Bitmap).GetHicon)

Ben C ,

Why do you prefer embedding vs adding as a resource?

Paul


Ben Hayat
Ben Hayat
Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)Advanced StrataFrame User (516 reputation)
Group: Forum Members
Posts: 374, Visits: 1.2K
Paul Chase (07/13/2007)
Ben H,



Here is how to accomplish what you originally asked in VB,For C# you would probably just have to change the casting.



NotifyIcon1.Icon = System.Drawing.Icon.FromHandle(CType(Me.ImageList1.Images(0), Drawing.Bitmap).GetHicon)



Ben C,



Why do you prefer embedding vs adding as a resource?



Paul




Paul, thank you very much for your contribution. I went ahead and did it in C# and works great.

If you don't mind, I'll post your solution along side Ben's in the sample area.



Once again, Thanks!

..ßen
StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
GetHIcon()

Nice, one Paul... I always forget about that one.

Why do you prefer embedding vs adding as a resource?

Well, they're essentiall the same thing... when you add it as a resource, it gets added to the .resx file, which itself is embedded.  Pretty much the same difference.

But the main reason I mentioned embedding is that for some reason, I was thinking that when you added icons to the project resources (or any other resx file), they were added as images.  However, I now see that there are separate categories for Images and Icons... so, I stand corrected Smile

So, for Ben H, it probably would be easier to add the icon as an ICO file to the Icons of your resources and just retrieve it through the default resource manager since it will create the strong-typed property for it.

Paul Chase
Paul Chase
Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)
Group: Forum Members
Posts: 414, Visits: 2.8K
Thanks Ben !

I wanted to make sure there was no gotcha that I didn't know about! Smile

Check out the trailers for Crysis http://www.crysis-online.com/

StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Check out the trailers for Crysis http://www.crysis-online.com/

Oh, yeah, that is going to be a beautiful game.  The guys over at Kotaku.com said they got a pretty good hands on at E3 last week and said that no only are the graphics and engine beautiful, but the gameplay is really going to shine as well.  For right now, though, I'd have to honestly say that I think I'm more interested in Halo 3 (duh...) and Mass Effect (masseffect.bioware.com) which is going to be just phenominal.

Ivan George Borges
Ivan George Borges
Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)
Group: StrataFrame MVPs
Posts: 1.9K, Visits: 21K
Hey Ben.

Have you played the Halo3 beta?

StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Have you played the Halo3 beta?

Oh, yeah, I racked up about 140 games on the 3 weeks that it was running... only played enough games to make Lieutenant, but I did have a kill/death ratio of about 2.3... something like 2300 kills and 900 deaths.  They took down the online stats so I don't remember what they were exactly. 

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search