Solved

Get Image Path from Resources

Posted on 2011-09-15
7
997 Views
Last Modified: 2012-08-13
Hello EE,

I have inserted into my Projet folder 2 images. I have then added them into Resources under Images.

now in my Code I would like get the string path of it, kind of:   My.Resources.thisPicture1.Path.ToString()

This is not valid but its an example for you.. I dont get how to retrive the path ?
0
Comment
Question by:PhilippeRenaud
  • 4
  • 3
7 Comments
 
LVL 40
ID: 36543913
You can't from the code.

When you embed an image, it becomes part of the .exe. The project's files keep a record of the path, but that information is not compiled into the application, so the code does not have access to it.

If you need to be able to access the image file at runtime, do not add it to the resources. Add the image file to your project and flag it as Content it the Build Action property of the file. When preparing your deployment, include the Content files.
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 36544002
Lets say my png file is with all my forms, inside my solution then.

If I do My.Application.Info.DirectoryPath  it brings me too far (at debug I think)

How do I get the Projet forms level ?
0
 
LVL 40
ID: 36544375
Let's take it from the start.

What are you doing with the image. Are you only showing it in your forms? Will it have to be modified after the application is distributed?

Why do not need the path? If the image is in the resources, you do not need the file.

If you have difficulty with English, answer in English first for everybody else on the site...

... and give me also you ranswer in French. It might be easier to get the details straight.

-----

... et donne-moi aussi ta réponse en français. Ce serait peut-être plus clair
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 36544502
I have a chart inside my form.

with the chart, inside my code, I have this line :

ctFinancial.Legends("Default").CustomItems(1).Cells(0).Image =   ...

.Image needs a String for the image (so the path of it)


I can't use the Resource because it will be an object
So I have place the image .png file located at the same place as my form.
Now I want to say:  

ctFinancial.Legends("Default").CustomItems(1).Cells(0).Image =   "the path of my image located at the same place as my form"

.. :)  I think it's clear? no need in french
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36544752
Yes, it's clear.

The easiest way to do that is to place the image file in the application directory or in a subdirectory of the application directory. The application directory is the default directory when an application starts, so you would not have to deal with the path.

While debugging, simply copy it in all the bin directories you have under your project.

Flag the file as Content as suggested in my first comment.

When you prepare your deployment, be sure to include the image file in the installation so that it will be in the same place relative to the application.
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 36549423
Yes its already flaged as Content.


Alors si je comprends il faudrait que je mettre mes png dans le bin folder ?? Me semble que ca va pas la..?


Ensuite je fais :   My.Application.Info.DirectoryPath & "pict.png"

qqch de meme ?
0
 
LVL 40
ID: 36550379
That's it. If you need a path, you need a file.

The limitation comes from your chart object. It should be able to accept an Image object, but from what you say it requires a String. Bad design in my opinion. A class should give some latitude to the programmers that use it.

Mixing images with executables is never a good thing however. What I do in my applications is that I often have a Data or Support or Images subdirectory in bin (and in the installation directory when I deploy). I leave bin to the compiler and put everything else in bin\Images

My.Application.Info.DirectoryPath & "\Images\pict.png"

Quelque chose de même.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question