-2

I have been tasked with creating BlackJack in C# for my final project. C# is fairly foreign to me so I'm having trouble with assigning images to my cards. Right now I have a cards class:

public class Card
{
    private string face;
    private string suit;

    public Card(string cardFace, string cardSuit)
    {
        face = cardFace;
        suit = cardSuit;
    }

    public override string ToString()
    {
        return face + " of " + suit;
    }
}

And I have Deck class:

public class Deck
{
    private Card[] deck;
    private int currentCard;
    private const int NUMBER_OF_CARDS = 52;
    private Random ranNum;

    public Deck()
    {
        string[] faces = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
        string[] suits = { "Hearts", "Clubs", "Diamonds", "Spades" };         

        deck = new Card[NUMBER_OF_CARDS];
        currentCard = 0;
        ranNum = new Random();
        for (int count = 0; count < deck.Length; count++)
            deck[count] = new Card(faces[count % 13], suits[count / 13]);
    }

    public void Shuffle()
    {
        currentCard = 0;
        for (int first = 0; first < deck.Length; first++)
        {
            int second = ranNum.Next(NUMBER_OF_CARDS);
            Card temp = deck[first];
            deck[first] = deck[second];
            deck[second] = temp;
        }
    }

    public Card DealCard()
    {
        if (currentCard < deck.Length)
            return deck[currentCard++];
        else
            return null;
    }
}

Finally, I have a simple Windows Form that has two buttons that simply deals a card and shuffles the deck with a label that shows the string of the current card that was dealt:

 public partial class Form1 : Form
{
    Deck deck = new Deck();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    }

    private void buttonDeal_Click(object sender, EventArgs e)
    {
        Card card = deck.DealCard();
        labelOutput.Text = card.ToString();
    }

    private void buttonShuffle_Click(object sender, EventArgs e)
    {
        deck.Shuffle();
    }
}

Now, I just need some way to assign images to my cards, and I'm not sure on the how.. Any help would be greatly appreciated.

EDIT: Here's what I've been trying to in my Card class.

   public static Image FromFile()
    {

        fileName = face + "_" + "of" + "_" +suit+".png";
        Image image = Image.FromFile(fileName);
        return image;
    }

I have an error saying that Resources does not have a definition for fileName.

I've added all the images as resources to the project and named all the files with the convention simliar to "Eight_of_Clubs".

EDIT: I've changed this in my Windows form:

 private void buttonDeal_Click(object sender, EventArgs e)
    {
        Card card = deck.DealCard();
        string fileName = card.getFace() + "_" + "of" + "_" + card.getSuit() + ".png";

        Image image = Image.FromFile(fileName);
        pictureBox1.Image = image;
        labelOutput.Text = card.ToString();
    }

But when I run the program and click the deal button i get the error: File does not exist exception

Which is false because there is a file loaded in with the resources for this project that is named King_of_Spades.png

JoeRamDo
  • 11
  • 2
  • 2
    There's literally a class called `Image`. Did you Google search *C# windows forms images* or something like that? Millions of results... Why bother asking? – Luke Joshua Park Dec 02 '15 at 20:38
  • Because I need to generate an image based off what card is drawn. Yea, I see how to manually add images to a picture box, but that wouldn't be helpful. – JoeRamDo Dec 03 '15 at 01:29
  • When you say generate, do you literally want to build the image yourself, or do you want to pull it from a file? – Luke Joshua Park Dec 03 '15 at 01:38
  • Pull it from a file, in fact, I've been somewhat running with the train of thought that since my Card object knows the string value of its suit and value I could attempt to have that correspond to the correct image (with the same file name as face + of + suit). – JoeRamDo Dec 03 '15 at 01:42
  • 1
    That's literally exactly what I was going to recommend. You can then use `Image.FromFile()` to pull the image. – Luke Joshua Park Dec 03 '15 at 01:43
  • 1
    I've been trying it out, but I've been running into problems. I'll make an edit and show you where I'm at. – JoeRamDo Dec 03 '15 at 01:46

1 Answers1

0

When you add an image to the resources of the assembly, it doesn't exist as a file on disk. Instead it exist as data on the generated binary. So, if you are not deploying the files with the binary, but addding them as resources instead, they will not exist as files on the deployment enviroment and thus Image.FromFile will not work.

You can fix this two ways: 1) deploy the files, set it on the properties of the file in your IDE to copy always. So they will be copied to the target folder when the binary is generated. 2) Since they are already resources, load them from there instead of loading them from file.

See Load image from resources area of project in C#

Community
  • 1
  • 1
Theraot
  • 31,890
  • 5
  • 57
  • 86