简体   繁体   中英

Image not displaying at runtime 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!

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"/>

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.

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.

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"/>

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM