Results 1 to 4 of 4

Thread: [RESOLVED] How do you get path to an image stored in resources

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Resolved [RESOLVED] How do you get path to an image stored in resources

    I have put various files into my application's Resources (pic1, pic2, pic3, etc.). Random images are placed into a picturebox control (pbxPic) on my form. When I click a button I want to identify the path to the picture file being displayed in pbxPic.

    I noticed that in the Resources.resx file that each entry has a <value> associated with it that identifies the path.
    Code:
    ...  
      <data name="pic1" type="System.Resources.ResXFileRef, System.Windows.Forms">
        <value>..\Resources\pic1.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
      </data>
      <data name="pic2" type="System.Resources.ResXFileRef, System.Windows.Forms">
        <value>..\Resources\pic2.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
      </data>
      <data name="pic3" type="System.Resources.ResXFileRef, System.Windows.Forms">
        <value>..\Resources\pic3.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
      </data>
    I was thinking that I could capture the associated value when I loaded the associated resource into my picturebox, but I do not know how to reference it. So for example
    Code:
    dim PicPath as string 
    
    pbxPic.image = My.Resources.pic1
    PicPath = ????????

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: How do you get path to an image stored in resources

    I'm wondering why you want the path.
    Once you've created an executable, the images are part of the executable so there is no path associated with the image. The path in the resx file is just for the IDE and compiler/linker so that the file can be pulled into or linked to the executable. You can copy the executable to a completely different place on your computer, or to another computer and it will be able to access the pictures which are part of the exe. The files don't need to exist on the computer to run the exe, only to build it, and the executable doesn't have a need or even a way to access the path of where the files originally came from. That information is superfluous to the executable.

    If you want to know which picture you loaded from the built in resources, then store that information. At the time you load the resource, you had to know its name, so save that, or something associated with that.

    Code:
    Dim PicPath As String 
    
    pbxPic.image = My.Resources.pic1
    PicPath = "pic1"
    
    'or store with picturebox, so no extra variable needed
    
    pbxPic.Image = My.Resources.pic1
    pbxPic.Tag = "pic1"
    Last edited by passel; Aug 28th, 2020 at 09:15 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: How do you get path to an image stored in resources

    The reason I want the file is I am creating an html report from within the application, and outputting HTML and CSS. I wanted to insert the picture into the report, which required the file name.

    I suspected the images were integrated into the exe, but was not sure. If I distribute the pictures with the exe, I do like your second approach using tag. I was not aware you could do that.

    Thanks for looking at it and giving me your feedback.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] How do you get path to an image stored in resources

    I'm not sure whether this will be of value but, if you want to use resources so that the user cannot delete or modify the images you distribute but you need a file with a path for some purpose, you can just save the resource to a temp file and then use that:
    vb.net Code:
    1. Dim tempFilePath = Path.GetTempFileName()
    2.  
    3. Using myImage = My.Resources.MyImage
    4.     myImage.Save(tempFilePath)
    5. End Using
    You can now use tempFilePath in HTML or whatever. Just be aware that you should endeavour to delete any temp files you create, so you should keep that path somewhere and delete the file when you're finished with it or when the application exits.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width