0

I'm using VS2013. .NetFramework 3 . I'm writing a simple project like as image gallery. I'm trying to add an JPEG image into Image Control. First I'm adding the test.jpg image as resource. Then I'm adding an Image Control on to Window. In the next step I'm selecting the image in "Source" property. The image displaying into design mode. All is ok. But when I'm running the project nothing displaying. I searched in google and youtube. I founded some solutions but I haven't solution for my problem (Sorry for ENG) Here the code line

<Image Source="pack://siteoforigin:,,,/Resources/1.JPG" ... />
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Please show us the relevant parts of your code. – Clemens May 31 '15 at 13:04
  • ok i added the line into question –  May 31 '15 at 13:10
  • I can't see any type of image I tested also with png image but I have the some problem. Maybe I have some mistakes in properties –  May 31 '15 at 13:11
  • WPF handles image resources differently than WinForms. You would not create an image resource, but just add an image file to your Visual Studio project and set its Build Action to Resource. See [this answer](http://stackoverflow.com/a/15008178/1136211) for more details. – Clemens May 31 '15 at 13:15
  • See also [this question and answer](http://stackoverflow.com/q/12690774/1136211). – Clemens May 31 '15 at 13:30
  • Thank you Clemens. My mistake is the difference between resourse.resx and Resources folder. Also I was not set the Buil in ACtion as "Resource". Now I have the solution. Thank you brother –  May 31 '15 at 13:40

1 Answers1

0

In order to simply display the image in WPF app, do the following:

1). In XAML, add:

<Image Name="imgName" Source="/ProjAssemblyName;component/RelativePathToImageFile" />

2). In Visual Studio IDE, select the image file and check its properties:

Build Action: Resources
Copy to Output Directory: Do Not Copy

Note: in most typical case the project Assembly name is the same as Default Namespace; check it in application property dialog. Also useful info pertinent to your case is included in the Microsoft reference (as pointed out by member @Clemens): https://msdn.microsoft.com/en-us/library/aa970069%28v=vs.110%29.aspx#Resource_File_Pack_URIs___Local_Assembly.).

Another option is to set the image Build Action property to EmbeddedResource and get the BitmapImage object corresponding to the image file programmatically like the following:

BitmapImage _bmpImage = new BitmapImage();
_bmpImage.BeginInit();
_bmpImage.StreamSource = Assembly.GetExecutingAssembly().GetManifestResourceStream(String.Concat(ProjAssemblyName, ImgFile));
_bmpImage.EndInit();

This technique is useful if further image processing is considered.

Hope this may help. Kind regards,

Alexander Bell
  • 7,842
  • 3
  • 26
  • 42
  • Where `YourProjNamespace` isn't a namespace, but actually an assembly name, and not necessary to be specified as long as the resource is in the same assembly. See [Resource File Pack URIs](https://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx#Resource_File_Pack_URIs___Local_Assembly). – Clemens May 31 '15 at 13:50
  • That's right: in most typical case these two names are the same. Best regards, – Alexander Bell May 31 '15 at 13:54