Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image not displaying in C# WPF

I have been trying to learn C# but I'm coming across a lot of problems. 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"

Thanks for your help!

like image 399
EthanBar Avatar asked Jun 04 '14 23:06

EthanBar


People also ask

How can I display image in C?

The C language itself doesn't have any built-in graphics capability. You can use a graphics toolkit like Qt, gtk, wxWidgets, etc. You could also construct an image file (BMP is pretty simple), and then launch (using fork and exec) an application to view it.

Why img src not working?

Img src Not Working That means, when a web page loads, the browser has to retrieve the image from a web server and display it on the page. The broken link icon means that the browser could not find the image. If you've just added the image, then check that you included the correct image URL in the source attribute.

Why image is not showing in WPF?

In your project: Create a folder say "img", right click on it and select add existing item add image to folder. Go to properties of the added image, set Build Action as Resource and Copy To Output Directory as Copy if newer .

Why is my image not showing in HTML?

Why Is My Image Not Showing up in HTML? One of the reasons why your HTML image not showing in browser is that its file is not located in the same folder that is indicated within your tag. Also, the image may not load because the file name specified in the <img> tag does not match that of your image file.


4 Answers

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 and Copy To Output Directory as Copy if newer.

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"/>
like image 92
swapnil Avatar answered Oct 01 '22 07:10

swapnil


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.

like image 32
Christoffer Eriksson Avatar answered Oct 01 '22 08:10

Christoffer Eriksson


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.

like image 43
Scott Madeux Avatar answered Oct 01 '22 07:10

Scott Madeux


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"/>
like image 20
Harjeet Singh Avatar answered Oct 01 '22 06:10

Harjeet Singh