Dynamically adding and loading images from resources in C #

I have some images added to my solution, right now it is under the images \ flowers \ rose.png folders inside the solution browser. I want to dynamically load this image into my image control.

My current approach is to make the content type and use the copy always properties. Then I would give a relative path to the image as shown below.

Image2.Source = new BitmapImage(new Uri("/images/flowers/Customswipe_b.png", UriKind.Relative)); 

Is there a way to download it from a resource without copying it to the target system.

+8
c # image resources wpf
source share
5 answers

The following works great for me:

 image.Source = new BitmapImage(new Uri("pack://application:,,,/YourAssemblyName;component/Resources/someimage.png", UriKind.Absolute)); 

You must also change the Build Action your image from None to Resource .

+14
source share

You can open the resource editor (solution explorer, click on Resource.resx) and add the image there. Then you can simply access it as a Bitmap using Properties.Resources.ImageId

http://msdn.microsoft.com/en-us/library/3bka19x4(v=vs.100).aspx

+5
source share

You use this:

  Image2.Source = new Bitmap( System.Reflection.Assembly.GetEntryAssembly(). GetManifestResourceStream("MyProject.Resources.myimage.png")); 

Or

 Image2.Source = new Bitmap(WindowsFormsApplication1.Properties.Resources.myimage); 

I recommend the second option.

+1
source share

How you add an image and then change its Create action to Resources also does the job. But since you asked to add and load from the Resource wold a different approach to achieve the same task. I would like to provide you with a link to read some msdn articles.

Adding and Editing Resources (Visual C #)

0
source share

I am having some problems finding the exact syntax for the URI, so see below for more details:

If your image ( myImage.png ) is in a subfolder of "images" (from the root directory), the exact syntax is:

 image.Source = new BitmapImage(new Uri(@"pack://application:,,,/images/myImage.png", UriKind.Absolute)); 

If your image is located in a subfolder of images/icon/ (from the root directory), the syntax is:

 image.Source = new BitmapImage(new Uri(@"pack://application:,,,/images/icon/myImage.png", UriKind.Absolute)); 
  • Note that the "pack://application:,,, does not change.
  • Be sure to set Build Action to Resources.

For more information: see here .

0
source share

All Articles