StrataFrame Forum
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      


12»»

How to convert a Bitmap to Icon at code...Expand / Collapse
Author
Message
Posted 07/12/2007 10:17:14 AM


StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: Forum Members
Last Login: 10/31/2007 5:20:05 PM
Posts: 374, Visits: 1,197
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
Post #10237
Posted 07/13/2007 9:17:31 AM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: 09/26/2008 8:30:36 AM
Posts: 2,685, Visits: 1,886
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.


www.bungie.net
Post #10250
Posted 07/13/2007 9:44:03 AM


StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: Forum Members
Last Login: 10/31/2007 5:20:05 PM
Posts: 374, Visits: 1,197
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
Post #10258
Posted 07/13/2007 1:33:55 PM


StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: Today @ 3:05:43 PM
Posts: 359, Visits: 2,308
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

Post #10280
Posted 07/13/2007 3:24:22 PM


StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: Forum Members
Last Login: 10/31/2007 5:20:05 PM
Posts: 374, Visits: 1,197
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
Post #10284
Posted 07/16/2007 8:59:29 AM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: 09/26/2008 8:30:36 AM
Posts: 2,685, Visits: 1,886
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

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.


www.bungie.net
Post #10299
Posted 07/16/2007 9:51:26 AM


StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: Today @ 3:05:43 PM
Posts: 359, Visits: 2,308
Thanks Ben !

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

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

Post #10307
Posted 07/16/2007 3:52:57 PM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: 09/26/2008 8:30:36 AM
Posts: 2,685, Visits: 1,886
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.


www.bungie.net
Post #10318
Posted 07/17/2007 9:23:08 AM


Advanced StrataFrame User

Advanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame User

Group: StrataFrame Users
Last Login: Today @ 4:47:31 PM
Posts: 700, Visits: 11,196
Hey Ben.

Have you played the Halo3 beta?

Post #10342