![](/img/trans.png)
[英]Refering from image resources to images in the packages folder in VS 2015
[英]Is there a more efficient way to get images from the resources folder than to manually assign each image?
我正在處理的程序是一副紙牌。 現在,所有證件圖像都分別分配到圖片框。 (給出的代碼只是其中的一小部分,但您會明白的,只是重復一遍,直到我到達King Card)。
因此,我的資源設置方式如下: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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.