Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否有比手动分配每个图像更有效的方法从资源文件夹获取图像?_C# - Fatal编程技术网

C# 是否有比手动分配每个图像更有效的方法从资源文件夹获取图像?

C# 是否有比手动分配每个图像更有效的方法从资源文件夹获取图像?,c#,C#,我正在处理的程序是一副纸牌。现在,所有的卡片图像都被分别分配到picturebox。给出的代码只是其中的一小部分,但你明白了,它只是一个重复,直到我拿到王牌 所以我的资源设置方式是这样的:Clubs_1、Clubs_2、Clubs_3等等。 我想知道是否有比写这么长的代码更快的方法 private void AssignCards(Card picCard, int count) { List<PictureBox> picPlayer = new List&

我正在处理的程序是一副纸牌。现在,所有的卡片图像都被分别分配到picturebox。给出的代码只是其中的一小部分,但你明白了,它只是一个重复,直到我拿到王牌

所以我的资源设置方式是这样的:Clubs_1、Clubs_2、Clubs_3等等。 我想知道是否有比写这么长的代码更快的方法

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;
        }
    }

查看生成的Resources.Designer.cs文件。您可以在解决方案资源管理器的Resources.resx下找到它,也可以在代码中选择一个资源名称,然后按F12转到定义

其中一个属性的定义如下所示:

/// <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));
    }
}
var img = (Bitmap)Properties.Resources.ResourceManager.GetObject("Image1", Properties.Resources.Culture);

您可以循环使用所有的组合,将每个图像的副本存储在查找表中,而不是每次需要时都获取一个新副本。

我认为这是最适合您的解决方案

                    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;

您可以改为使用Properties.Resources.Suits[picCard.Suit][1]之类的操作吗?样板是使用一个图像,它的卡片面以13x4的网格排列。Graphics.DrawImage重载两个矩形参数,以复制所需的矩形参数。除了让项目资源管理变得不那么痛苦之外,它还会自动迫使您编写枯燥的代码。非常确定OP使用的是WinformsIm,很高兴它能与String.Format一起工作,现在我可以访问任何资源了。对于想知道我使用了什么代码的人:Image cardImage=BitmapProperties.Resources.ResourceManager.GetObjectString.Format{0},replaceString;