Adding Menu Item Images from Embedded Resources

2 posts, 0 answers
  1. Marbry
    Marbry avatar
    123 posts
    Member since:
    Aug 2012

    Posted 03 Oct 2013 Link to this post

    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?
  2. Marbry
    Marbry avatar
    123 posts
    Member since:
    Aug 2012

    Posted 04 Oct 2013 Link to this post

    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.
  3. DevCraft R3 2016 release webinar banner
Back to Top