Labib Zaman
Labib Zaman

Reputation: 5

Is there a more efficient way to get images from the resources folder than to manually assign each image?

The program I'm dealing with is a deck of cards. And right now, all the card images are assigned to the picturebox individually. (The code given is just a small part of it but you get the idea, its just a repetition till I reach the King Card).

So the way my resources are set up is like this: Clubs_1, Clubs_2, Clubs_3, etc. I was wondering if there's a faster way than having to write this long code.

private void AssignCards(Card picCard, int count)
    {
        List<PictureBox> picPlayer = new List<PictureBox>() { picDealer1, picDealer2, picPlayer1, picPlayer2 };

        if (clickCount == 1)
            picPlayer.Add(picPlayer3);
        if (clickCount == 2)
        {
            picPlayer.Add(picPlayer3);
            picPlayer.Add(picPlayer4);
        }

        switch (picCard.Face)
        {
            case "Ace":
                if (count <= 1)
                    computer.Score++;
                else
                    player.Score++;

                if (picCard.Suit == "Hearts")
                    picPlayer[count].Image = Properties.Resources.Hearts_1;
                else if (picCard.Suit == "Diamonds")
                    picPlayer[count].Image = Properties.Resources.Diamonds_1;
                else if (picCard.Suit == "Clubs")
                    picPlayer[count].Image = Properties.Resources.Clubs_1;
                else if (picCard.Suit == "Spades")
                    picPlayer[count].Image = Properties.Resources.Spades_1;
                break;
            case "Deuce":
                if (count <= 1)
                    computer.Score += 2;
                else
                    player.Score += 2;

                if (picCard.Suit == "Hearts")
                    picPlayer[count].Image = Properties.Resources.Hearts_2;
                else if (picCard.Suit == "Diamonds")
                    picPlayer[count].Image = Properties.Resources.Diamonds_2;
                else if (picCard.Suit == "Clubs")
                    picPlayer[count].Image = Properties.Resources.Clubs_2;
                else if (picCard.Suit == "Spades")
                    picPlayer[count].Image = Properties.Resources.Spades_2;
                break;
            case "Three":
                if (count <= 1)
                    computer.Score += 3;
                else
                    player.Score += 3;

                if (picCard.Suit == "Hearts")
                    picPlayer[count].Image = Properties.Resources.Hearts_3;
                else if (picCard.Suit == "Diamonds")
                    picPlayer[count].Image = Properties.Resources.Diamonds_3;
                else if (picCard.Suit == "Clubs")
                    picPlayer[count].Image = Properties.Resources.Clubs_3;
                else if (picCard.Suit == "Spades")
                    picPlayer[count].Image = Properties.Resources.Spades_3;
                break;
        }
    }

Upvotes: 0

Views: 194

Answers (2)

zaman
zaman

Reputation: 145

I thik this is best solution for you

                    byte[] imageArray = System.IO.File.ReadAllBytes(Server.MapPath("~/App_Data/Images/client_group_logo.png"));
                    var imageString = imageArray != null ? Convert.ToBase64String(imageArray) : "";
                    var img = string.Format("data:image/jpg;base64,{0}", imageString);
                    ViewBag.ImagePath = img;

Upvotes: -1

Billy
Billy

Reputation: 330

Take a look in the generated Resources.Designer.cs file. You can find it under Resources.resx in the solution explorer or you can select one of the resource names in your code and press F12 (go to definition).

The definition for one of the properties looks something like this:

/// <summary>
///   Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap Image1 {
    get {
        object obj = ResourceManager.GetObject("Image1", resourceCulture);
        return ((System.Drawing.Bitmap)(obj));
    }
}

From outside the class, you can get a resource by string name with something like this:

var img = (Bitmap)Properties.Resources.ResourceManager.GetObject("Image1", Properties.Resources.Culture);

You can loop through all the combinations are store a copy of each image in a lookup table rather than fetching a fresh copy each time you need it.

Upvotes: 2

Related Questions