2016-12-08 92 views
0

我是C#的初學者,我對將圖像從圖像列表添加到類中有疑問。 我擁有的課程用於添加這些圖像,這些圖像是包含每張卡片圖像,套裝名稱,面值和點值的「卡片」。我有一個正在進行的工作代碼來添加每張卡片的套裝名稱和麪值,但是我想知道爲了將圖像從圖像列表添加到類中,是否可以使用「for」循環來添加每張圖像? 此外,我想補充的新的類的對象實例至少52卡......將圖像列表中的圖像添加到類中

下面包含我當前的代碼:

private void FormShuffleCardDeck_Load(object sender, EventArgs e) 
    { 
     string[] suitList = new string[4]; 
     string[] faceList = new string[13]; 
     int[] pointValues = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; 
     string face = ""; 
     int counter = 0; 
     for (int i = 0; i < 4; i++) 
      { 
       suitList[i] = i.ToString(); 
       switch (suitList[i]) 
       { 
        case "0": 
         suitList[0] = "Hearts"; 
         break; 
        case "1": 
         suitList[1] = "Clubs"; 
         break; 
        case "2": 
         suitList[2] = "Diamonds"; 
         break; 
        case "3": 
         suitList[3] = "Spades"; 
         break; 
       } 
       for (int k = 0; k < 13; k++) 
       { 
        // face = k.ToString(); 
        faceList[k] = k.ToString(); 
        switch (faceList[k]) 
        { 
         case "0": 
          faceList[0] = "2"; 
          break; 
         case "1": 
          faceList[1] = "3"; 
          break; 
         case "2": 
          faceList[2] = "4"; 
          break; 
         case "3": 
          faceList[3] = "5"; 
          break; 
         case "4": 
          faceList[4] = "6"; 
          break; 
         case "5": 
          faceList[5] = "7"; 
          break; 
         case "6": 
          faceList[6] = "8"; 
          break; 
         case "7": 
          faceList[7] = "9"; 
          break; 
         case "8": 
          faceList[8] = "10"; 
          break; 
         case "9": 
          faceList[9] = "Jack"; 
          break; 
         case "10": 
          faceList[10] = "Queen"; 
          break; 
         case "11": 
          faceList[11] = "King"; 
          break; 
         case "12": 
          faceList[12] = "Ace"; 
          break; 
        } 
        cardDeckList[counter] = new PlayingCard(suitList[i], faceList[k], imageListCards.Images[counter], 1); 
        counter++; 
        listBoxOutput.Items.Add(cardDeckList[counter].ToString()); 
       } 
      } 
    } 

編輯:我補充說,我的工作一類上。

public class PlayingCard 
{ 
    /// <summary> 
    /// Fields: used to store the data about a Playing card (private access for security) 
    /// </summary> 
    private string _faceValue; // face value  
    private string _suit;   // suit value 
    private Image _cardImage; // image of card 
    private double _pointValue; // point Value 

    /// <summary> 
    /// Properties: used to access the fields (get = read, set = modify or write) 
    /// </summary> 
    public string Suit 
    { 
     get { return _suit; } 
     set { _suit = value; } 
    } 
    public string FaceValue 
    { 
     get { return _faceValue; } 
     set { _faceValue = value; } 
    } 
    public Image CardImage 
    { 
     get { return _cardImage; } 
     set { _cardImage = value; } 
    } 
    public double PointValue 
    { 
     get { return _pointValue; } 
     set { _pointValue = value; } 
    } 

    /// <summary> 
    /// The constructor is a special method that instantiates PlayingCard objects (e.g. Ace of Spades). 
    /// </summary> 
    /// <param name="suit">The suit of the playing card (e.g. Hearts, Clubs, Diamonds and Spades)</param> 
    /// <param name="faceValue">The face value of the playing card (numbers 2-10, Jack, Queen, King, Ace)</param> 
    /// <param name="cardImage">The image of the card</param> 
    /// <param name="pointValue">The point value of the card</param> 
    public PlayingCard(string suit, string faceValue, Image cardImage, double pointValue) 
    { 
     _suit = suit; 
     _faceValue = faceValue; 
     _cardImage = cardImage; 
     _pointValue = pointValue; 
    } 
    } 
+0

你是否在談論一個[ImageList](https://msdn.microsoft.com/en-us/library/system.windows.forms.imagelist( v = vs.110)的.aspx)? – TaW

+0

是的,它是一個ImageList。 –

回答

0

據我瞭解你的項目,你想創建一個張牌, 吧? 我以爲我設計了一些小課程,可以幫助您更輕鬆地處理您的代碼和想法。

一開始,我會建議從一開始就適合完美的卡片。

public class Card 
{ 
    public string Name { get; set; } 
    public int FaceValue { get; set; } 
    public int PointValue { get; set; } 
    public string Name { get; set; } 
    public byte[] image { get; set; } 
} 

以及你的套牌。可能不難,但將更容易存儲您的卡或與更多的卡工作,而不會放鬆您的思想。

public class Deck 
{ 
    public string Name { get; set; } 
    public ICollection<Card> Cards { get; set; } 
} 

添加Using System.Linq;方便地在甲板上的東西,如搜索卡:

Card tmpCard = tmpDeck.Cards.Where(w => w.Name == "MyFavouriteCard").FirstOrDefault();

這應該使它更容易讓你得到的牌出你的甲板。

我們您的名片圖像保存到你的類:)

public static byte[] ImageToByte(Image pImage) 
{ 
ImageConverter tmpConverter = new ImageConverter(); 
return (byte[])tmpConverter.ConvertTo(pImage, typeof(byte[])); 
} 

和你保存的圖像轉換回位圖。與將其加載到字節數組中一樣簡單:

public static Bitmap ByteToImage(byte[] pImage) 
{ 
    MemoryStream tmpStream = new MemoryStream(); 
    tmpStream.Write(pImage, 0, Convert.ToInt32(pImage.Length)); 
    Bitmap tmpBitmap = new Bitmap(tmpStream, false); 
    tmpStream.Dispose(); 
    return tmpBitmap; 
} 

將返回的位圖加載到圖片框中作爲示例。

希望這會幫助你一點點與您的項目。或至少現在你知道如何獲得一些卡在一起:)

相關問題