将图像列表中的图像添加到班级

时间:2016-12-08 06:46:23

标签: c# image class picturebox imagelist

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

1 个答案:

答案 0 :(得分: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;
}

将返回的位图加载到图片框中作为示例。

希望这对您的项目有所帮助。或者至少现在你知道如何将一些卡片放在一起了:)