none
Access resources via string?

    Question

  • Hello I'm a noob C# programmer so forgive me if I use the wrong terminology. 

    I have little understanding of how resources work, but it's proving to be sort of a pain.  I am trying to load an image into a picture box via:

    PictureBox1.Image = (System.Drawing.Image) WindowsControlLibrary2.Properties.Resources.Banana;

    This works fine.  However I won't to be able to load an image based on a STRING... for example I was loading it this way before:

    int someString \\(ie: "Cherry", "Kiwiw", etc.)
    PictureBox1.Image = System.Drawing.Image.FromFile("C:\\Projects\\BlackJack\\BlackJack\\Resources\\" + someString + ".png");

    However I understand that this is a dangerous way in doing it and I want to do it the right way.

    Can someone give me a hint on how this should be done?

    Thank you



    Monday, August 25, 2008 11:01 PM

Answers

  • Try this:

            public static Image GetImage(string name)  
            {  
                return (Image)typeof(WindowsControlLibrary2.Properties.Resources).GetProperty(name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).GetValue(nullnull);  
            } 

    David Morton - http://blog.davemorton.net/
    Tuesday, August 26, 2008 4:30 PM
    Moderator

All replies

  •  If I understand you correctly you want to have a reference path shaped dynamically at runtime. There is nothing wrong with it. Relax and go ahead with clear conscience. You may be better of if you use StringBuilder class for that as well. A rather elegant way of doing things.
    AlexB
    Tuesday, August 26, 2008 2:00 AM
  • String builder class?  Well I'd rather do it dynamically... I mean if the path to the images change it could promise to be problematic... and to be honest I ran into the problem.  I built a project and sent it to someone and all the images were never included... and it keeps the path names so the project errored out.
    Tuesday, August 26, 2008 2:31 AM
  • Hi,

    You can do it something similar as following - this is WPF:

                String absUri = HtmlPage.Document.DocumentUri.AbsoluteUri;  
                absUriabsUri = absUri.Remove(absUri.LastIndexOf("/") + 1, absUri.Length - (absUri.LastIndexOf("/") + 1));  
                UserImage.Source = new BitmapImage(new Uri(absUri + LoggedInUser.ImageURL, UriKind.Absolute));  
     

    If you are using something else than WPF, let me know.

    Thanks,
    Viral.
    Tuesday, August 26, 2008 6:35 AM
  •  String builder class?  Well I'd rather do it dynamically... I mean if the path to the images change it could promise to be problematic... and to be honest I ran into the problem.  I built a project and sent it to someone and all the images were never included...

    What a funny answer! Using StringBuilder is to do it dynamically and the fact that your strings lost parts thereof simply means that you did not do it correctly. You screwed them. I will bet a hundred dollars that you did.. Check your code.

    AlexB
    Tuesday, August 26, 2008 3:02 PM
  • What a funny answer! Using StringBuilder is to do it dynamically and the fact that your strings lost parts thereof simply means that you did not do it correctly. You screwed them. I will bet a hundred dollars that you did.. Check your code.


    AlexB



    Sorry that's not what I meant (i'm a noob).  I meant I don't know what a string builder class is and (well i'd rather do it dynamically) was in reference to what you said before.   Let me rephrase what I'm trying to do...   I want to use this statement:

    PictureBox1.Image = (System.Drawing.Image) WindowsControlLibrary2.Properties.Resources.Banana;

    Except I want that statement in a method where that takes in a string and I want (.Banana) to be arbitrary... so let's say I pass a string in to the method that is 'Cherry'  then I would want:

    PictureBox1.Image = (System.Drawing.Image) WindowsControlLibrary2.Properties.Resources.Cherry;

    If I was to do it this way THEN in effect it would be similar to this:

    int someString \\(ie: "Cherry", "Kiwiw", etc.)
    PictureBox1.Image = System.Drawing.Image.FromFile("C:\\Projects\\BlackJack\\BlackJack\\Resources\\" + someString + ".png");

    My reasons for this are specific but I don't feel there's reason to get into why I want to do it this way.

    Thanks.
    • Edited by CommanderKeen Tuesday, August 26, 2008 4:05 PM Spelling Mistakes
    Tuesday, August 26, 2008 4:01 PM
  • Viral Jain said:

    Hi,

    You can do it something similar as following - this is WPF:

                String absUri = HtmlPage.Document.DocumentUri.AbsoluteUri;  
                absUriabsUri = absUri.Remove(absUri.LastIndexOf("/") + 1, absUri.Length - (absUri.LastIndexOf("/") + 1));  
                UserImage.Source = new BitmapImage(new Uri(absUri + LoggedInUser.ImageURL, UriKind.Absolute));  
     

    If you are using something else than WPF, let me know.

    Thanks,
    Viral.



    Sorry I'm not even sure what WPF is....  But thank you I will analyze this code.
    Tuesday, August 26, 2008 4:03 PM
  • Just out of curiosity, why do you need to load the objects based on string?  Image.FromFile only works when you have a filepath, which means you're going to have to ensure the file exists at the path you're creating the file from, regardless of how you cut it.  I think you have your answer with Properties.Resources, personally, and that would be a much cleaner and much more manageable way to handle something like this. 
    David Morton - http://blog.davemorton.net/
    Tuesday, August 26, 2008 4:06 PM
    Moderator
  • I don't think I'm being clear in my example. 

    David M Morton said:

    Just out of curiosity, why do you need to load the objects based on string?  Image.FromFile only works when you have a filepath, which means you're going to have to ensure the file exists at the path you're creating the file from, regardless of how you cut it.  I think you have your answer with Properties.Resources, personally, and that would be a much cleaner and much more manageable way to handle something like this. 


    David Morton - http://blog.davemorton.net/



    I don't want to use Image.FromFile. 

    I want to USE Properties.Resources; however, I want the same control with the line:

    PictureBox1.Image = (System.Drawing.Image) WindowsControlLibrary2.Properties.Resources.Banana;

    as I have with:

    PictureBox1.Image = System.Drawing.Image.FromFile("C:\\Projects\\BlackJack\\BlackJack\\Resources\\" + someString + ".png");


    so in effect (I know this is syntactly incorrect) I want to achieve this:

    PictureBox1.Image = (System.Drawing.Image) WindowsControlLibrary2.Properties.Resources."someString";

    so (.Banana) can be an arbitrary string.

    Thanks, and hope this clarifies.
    Tuesday, August 26, 2008 4:10 PM
  •  Why don't you show me a collection of two or three strings you do want to use, I will recognize common elements and show you an example of a StringBuilder that will do it dynamically. What StringBuilder does: it takes a base string and you can add some string parts to it. It is very convenient to do it in a loop when those string parts are stored in a string array for example. I cannot figure out from your explanation what you want to accomplish. I kind of have a feel for that but it is too obscure.
    AlexB
    Tuesday, August 26, 2008 4:10 PM
  • Try something like the code in this post:

    http://forums.msdn.microsoft.com/en-US/netfxbcl/thread/d9f9e55a-ad88-4293-95e4-9c7d2f41a50a/
    David Morton - http://blog.davemorton.net/
    Tuesday, August 26, 2008 4:13 PM
    Moderator
  • Okay let me show an example:

    String someString;

    if(someString == "Cherry")
        PictureBox1.Image = (System.Drawing.Image) WindowsControlLibrary2.Properties.Resources.Cherry;

    if(someString == "Banana")
        PictureBox1.Image = (System.Drawing.Image) WindowsControlLibrary2.Properties.Resources.Banana;

    if(someString == "Kiwi")
        PictureBox1.Image = (System.Drawing.Image) WindowsControlLibrary2.Properties.Resources.Kiwi;

    This is redundant and bulky, I could achieve the same thing with:

    String someString
    PictureBox1.Image = assignImage(someString)

    public PictureBox assignImage(string exampleString)
    {
    return (System.Drawing.Image) WindowsControlLibrary2.Properties.Resources + "exampleString";
    }

    so i would be:
    PictureBox1.Image = (System.Drawing.Image) WindowsControlLibrary2.Properties.Resources.exampleString;


    I hope this clarifies.  Thanks for your patience.

    -CK





    Tuesday, August 26, 2008 4:16 PM
  • Try this:

            public static Image GetImage(string name)  
            {  
                return (Image)typeof(WindowsControlLibrary2.Properties.Resources).GetProperty(name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).GetValue(nullnull);  
            } 

    David Morton - http://blog.davemorton.net/
    Tuesday, August 26, 2008 4:30 PM
    Moderator
  •  
    David M Morton said:

    Try this:

            public static Image GetImage(string name)  
            {  
                return (Image)typeof(WindowsControlLibrary2.Properties.Resources).GetProperty(name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).GetValue(nullnull);  
            } 

    David Morton - http://blog.davemorton.net/



    Oh man!  You are the KING!!! 

    Thansk a lot it worked like a charm.
    Tuesday, August 26, 2008 4:41 PM