Image not displaying at runtime C# WPF
Asked Answered
L

14

32

I am trying to display an image in WPF but for some reason, the image won't show! It appears on the Visual Studio editor but when I run the application it doesn't appear.

Here is some of my code:

This is how I'm trying to display the image:

<Image HorizontalAlignment="Left" Height="100" Margin="273,147,0,0" 
       VerticalAlignment="Top" Width="100" Source="image.jpg"/>

I have also tried using this:

Source="pack://application:,,,/image.jpg"
Lawler answered 4/6, 2014 at 23:56 Comment(2)
Have you tried searching SO? A quick search which may be of help reveals : #11881446 and #15636547Concentration
The trick is to add the image file to your Visual Studio project and set its Build Action to Resource (which it should be by default).Karlakarlan
S
94

In your project:

  1. Create a folder say "img", right click on it and select add existing item add image to folder
  2. Go to properties of the added image, set Build Action as Resource.

It worked for me.

In XAML

<Image HorizontalAlignment="Left"  Name="MyImg" Height="80" Margin="273,147,0,0" 
    VerticalAlignment="Top" Width="100" Source="/img/Desert.jpg"/>
Smoke answered 5/6, 2014 at 7:24 Comment(5)
I had to set the Build Action to Embedded ResourceWhisky
I had to do several builds and rebuilds also cleaned up the bin and obj folder and finally checked the option "Copy always" for "Output Directory" to make it work eventually. And I can say the Image Name, Width, Height, etc. are not playing a role for displaying the image. What is essential here is to make the VS finally build the correct version with the image embedded in the executable.Wrestling
For that Path you do not need to set to Resource, Content is enough. If you set it to Resource and 'Copy to output directory' true it will be compiled into the assembly and additionally copied to the output directory - so effectively shipped twice.Centesimo
I also had to Build -> Clean Solution, Build Solution to make it work.Stocks
Seems that someone at M$ still hasn't gotten the 'memo' - you MUST CLEAN and then RE-BUILD for the changes to take effect. Seems curiously odd, but this is what works.Tenet
T
19

If none of these work, try changing the Build Action to Content.

That's what worked for me after struggling for a long time with this.

Tumid answered 14/7, 2021 at 9:41 Comment(3)
Thanks. Tried Resource and Embedded Resource as suggested here and Content is the one that worked for me with .NET 6.Filing
Content also works in VS 2022 with .NET 6.Permittivity
@MikeUpjohn Same for me. VS2022, tried most options and Content is the way to go. Maybe one time I will actually read up on what they mean :-))Oireachtas
R
13

Go to the properties for the image in Visual Studio and change "Build Action" to "Resource" and "Copy to Output Directory" to "Copy if newer".

I had to do a rebuild, but then it worked. Cred to swapnil.

Renfrew answered 28/3, 2018 at 5:58 Comment(0)
T
2

please drag the image to a image source,it will be like this /img/image.jpg"/

<Image HorizontalAlignment="Left" Height="100" Margin="273,147,0,0" 
       VerticalAlignment="Top" Width="100" Source="/img/image.jpg"/>
Torrance answered 5/6, 2014 at 6:10 Comment(1)
i did this bt it didn't work. syntax is correct but you nedd to change image properties.Smoke
R
2
  1. Right click your project name in solution explorer and select Add -> New Folder.
  2. Name this folder as Images
  3. Right click to Images folder and select Add -> Existing item and then select your image.png file
  4. On your image.png properties adjust build action -> Resource
  5. Now your image source is Source="Images/image.jpg"
  6. You should see your image on design window of VS. Do not forget to clean code before compiling
Robomb answered 27/4, 2023 at 7:19 Comment(0)
O
1

If none of the above works, try Rebuilding your application from the build menu. Not "Build", but "Rebuild"

Oviduct answered 29/4, 2022 at 2:45 Comment(0)
L
1

For example, this is your project structure

+ProjectName
+--+imagesFolder
|  +--amogus.png
|
+--App.xaml
+--MainWindow.xaml
+--etc.

and you want to access the to amogus.png in your xaml window, You have two ways:

note this way the imagesFolder will be visible in the release build to users

  1. to set amogus.png Build Action to Content and Copy to Output Directory to Copy always more info, then rebuild from the build menu, then add this to the window xaml
    <Image Source="pack://siteoforigin:,,/imagesFolder/amogus.png" ></Image>

note this way the imagesFolder will be not visible in the release build to users

  1. to set amogus.png Build Action to Resource and Copy to Output Directory to Do not copy or blank more info, then rebuild from the build menu, then add this to the window xaml
    <Image Source="/imagesFolder/amogus.png" ></Image>

more detail

Latakia answered 28/5, 2022 at 0:29 Comment(0)
R
0

Right click images on the Solution Explorer, choose Properties and then set the Build Action as Resource.

Reenareenforce answered 31/8, 2022 at 8:16 Comment(0)
W
0

Did not have to do a clean and rebuild. I tried every combination listed above (I am in VS2017)

  1. Go to Project->Properties->Resources
  2. Select File (drop down with choices of strings, images, icons...)
  3. Click Add Resource->Existing File
  4. Navigate to the image and import it
  5. VS identifies it as an image (mine is PNG) and switches the view to show Image resources
  6. Select the thumbnail of the image and in the Properties of the Image (type should be Bitmap) set Persistence to Embedded in resx
  7. I saved and closed Project Properties as I got confused here before
  8. Go to the Resources folder under the project and select the image (it should be listed for you)
  9. Select the image and set the BuildAction to Embedded Resource
  10. I set the File Action to Copy if Newer

From here I move back and forth between Debug and runtime, various combinations of clean, build and publish and the image has FINALLY been displayed every time.

Last tidbit, the XAML in the dialog looks like this:

<Image Source="pack://siteoforigin:,,,/Resources/DeathSpiral.png" />

I have updated several projects that were supposed to display graphics but didn't always do so using the steps above. They all work now. Tested in both VS2017 and VS2019 and no errors so far.

Wraparound answered 8/9, 2022 at 22:57 Comment(0)
E
0

I ended up solving this by using relative pathing in XAML:

<Image Source="../../Assets/QuickSettingsScreenImage.png"/>

And in the csproj file creating an ItemGroup, and setting the images as Resource

<ItemGroup>
    <Resource Include="Assets\QuickSettingsScreenImage.png" />
</ItemGroup>

After making the changes, I had to use the Rebuild Project button to get the image showing up correctly.
As a note, I am on dotnet 6

Emission answered 12/4, 2023 at 15:29 Comment(0)
E
0

Copy the image into the project and set its Properties\Build Action to Resource (Do not copy). Use this for the xaml part.

<Image Source="/<asm_name>;component/<image prj_path>"/>

<!-- this also works -->
<Image Source="/<image prj_path>"/>

If using the former, refer to the project's Properties\Application tab to get the assembly's exact name.

Economist answered 20/5, 2023 at 15:50 Comment(0)
H
0

for those who didn't get it to work after trying all these answers, I found this in my .csproj file and just remove it, it works!!

  <ItemGroup>
    <None Remove="Resources\img1.png" />
  </ItemGroup>
Hermon answered 2/9, 2023 at 8:57 Comment(0)
W
0

In my case, the problem was that I was trying to include a .svg file, which apparently isn't supported by default. By only changing from a svg file to jpg file with everything else staying the same, my image suddenly appeared.

Warmblooded answered 5/2 at 13:28 Comment(0)
L
0

I'm using the latest version of .net 8 this one works for me

  1. Create a folder name image and put there the image file
  2. Right click on the png/jpg file and go on properties
  3. Set the build action to Content and Copy to Output Directory to Copy Always.
Lifeblood answered 17/6 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.