This is a migrated thread and some comments may be shown as answers.

Adding Menu Item Images from Embedded Resources

1 Answer 14 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Marbry
Top achievements
Rank 1
Marbry asked on 03 Oct 2013, 08:19 PM
I'm building menus in code.  I need to be able to set the menu item image, but the only way I see to do this is by providing a URL string.

The problem is that the images are embedded resources, not separate files on the file system.  So I would like to be able to do something like...

Assembly assembly = Assembly.GetExecutingAssembly();
Stream imageStream = assembly.GetManifestResourceStream(ResourceName);
                                     
menuItem.ImageUrl(or other property) = new Bitmap(imageStream);

Obviously that won't work.  Is there a way to do this by providing the image data directly rather than a URL to a file?

1 Answer, 1 is accepted

Sort by
0
Marbry
Top achievements
Rank 1
answered on 04 Oct 2013, 01:50 PM
Well, I learned something.  There is a method to get a URL to an embedded resource, it's just something I'd never used.  This makes sense though, Telerik uses a lot of embedded resources, you'll see a lot of WebResource.axd requests.

this.ClientScript.GetWebResourceUrl(typeof(MyClassName), "ProjectDefaultNamespace.FolderPath.FileName.fileextension");


Although I did have to manually add a reference like below for each file within the AssemblyInfo.cs file which is kind of a pain when you have a lot of them.  It still generates a URL, but it didn't seem to actually point to anything until I did this.  I'm not sure if this is just from having to specify the mime type, or if it simply doesn't recognize the reference.  I also had to close the project and re-open it after modifying the AssemblyInfo.cs file to get it to pick up the changes.

[assembly: System.Web.UI.WebResource("ProjectDefaultNamespace.FolderPath.FileName.fileextension", "img/png")]

If someone knows of a better way to get it to pick up those embedded references without the manual editing I'd be happy to know what it might be.
Tags
Menu
Asked by
Marbry
Top achievements
Rank 1
Answers by
Marbry
Top achievements
Rank 1
Share this question
or