0

Im developing a card app. The app shall show two random cards on a table.

Ive created and image card deck to show the specific cards on the screen. Each card is an individual jpg file. The jpg files are all in a "Images" directory, which was created in the project directory.

The application starts with 2 default card images.

During the card drawing process, the new two images shall be seen.

To change the picture of the card1, I tried this:

string card1_url = "/Ah.jpg"; // Default value 

// Image-Path for new cards

if (card1.ToString() == "Ah") {  card1_url = "Images/Ah.jpg"; }
if (card1.ToString() == "Kh") {  card1_url = "Images/Kh.jpg"; }
...

Image card1_image = (Image)this.FindName("Card1_img");

Uri card1Uri = new Uri(card1_url, UriKind.Relative);

BitmapImage newImage = new BitmapImage(card1Uri);

card1_image.Source = newImage;

The application starts with no errors, but I see no card picture after dealing.

The URI looks correct.

How can I make sure, that the URI is correct? Is something missing? How can I fix it?

Spinshot
  • 275
  • 2
  • 12
  • 1
    As a note, all these `if` statements are highly redundant. Just write `card1_url = $"Images/{card1}.jpg";` – Clemens May 06 '23 at 15:39
  • In order to properly load image resource files, use [Resource File Pack URIs](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/app-development/pack-uris-in-wpf?view=netframeworkdesktop-4.8). – Clemens May 06 '23 at 15:42

1 Answers1

0

The code was right.

Pictures have to be tagged as "Ressource" in the properties windows (build process) in order to be loaded.

Now it works.

Spinshot
  • 275
  • 2
  • 12