0

I am using Visual Studio 2010 and C#. to build an Add-Im. I am trying to utilize/call images in my code that reside in my Images folder. So, in the solution explorer, I have added some images to my 'Images' folder. I also have a multi item list that generates multiple items in a menu (depending on a loop counter). Code below

Item item = new Item();
item.Caption = Convert.ToString(_propertySet.GetProperty(ELEMENT_NAME));
item.Message = Convert.ToString(_propertySet.GetProperty(ELEMENT_NAME));
item.Enabled = true;
item.Image = Image.FromFile(@"C:\Users\MyId\Desktop\Project\Stuff\Images\cmdContextMenu.png");
item.Tag = _propertySet.GetProperty(ELEMENT_NAME);

I use the entire path on MY machine to get to the image and reference it from the 'Images' folder. Now, if I were to finish this up, package it up, and move it other peoples machines, i'm thinking there would be problems with trying to find this image. Is there some kind of solution to this issue or a way of not having to reference the entire path?? Its kind of a vague question/scenario but any help on this would be greatly appreciated. Thanks in advance.

user1898629
  • 329
  • 1
  • 4
  • 22
  • you will definitely have a problem on this line `Image.FromFile` because not all machines have the same file path that you have also how come you can load images from a Resource File..have you considered that..? – MethodMan Nov 20 '14 at 16:50
  • What do you mean. I don't quite understand what you mean about loading images from a resource file. But yes, I agree that the Image.FromFile aspect will fail when moving to another machine – user1898629 Nov 20 '14 at 16:55

1 Answers1

2

I think you have 2 possibilities for this scenario: 1) The image is static, and deployed with the add-in, or 2) The image is customized per user.

If it's customised per user, you will need them to place the image in that folder. Which means you should really configure that location.

But I'm guessing it's a static image. In which case you simply need to add it as a resource, and reference that.

This has been covered in other posts: How to create and use resources in .NET

Community
  • 1
  • 1
JamesDill
  • 1,857
  • 1
  • 18
  • 22
  • So I followed the link provided. I added a resource and just called it in my code. Seems too good to be true since it worked so well. But thanks for your help again. You've saved me hours of research and work. Thanks again! – user1898629 Nov 20 '14 at 17:10