Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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#.net:将图像添加到列表并在表单上显示_C#_.net - Fatal编程技术网

C#.net:将图像添加到列表并在表单上显示

C#.net:将图像添加到列表并在表单上显示,c#,.net,C#,.net,我正在创建一个宠物列表,并尝试为每只宠物添加照片。我的列表还在运行,但我以前从未在c#中使用过图像,所以我有点卡住了 到目前为止,我已经有了从超级宠物类继承的品种类(虎斑猫、暹罗猫、哈士奇猫和奇瓦瓦猫)。以下是pet类中的属性: 字符串名称、长芯片、DateTime arrivalDate、Boolean adoptedStatus、, 图像 在我的主代码文件中,我创建了一些新宠物,然后将它们添加到宠物列表中: Tabby newTabby1 = new Tabby("sunshine", 22

我正在创建一个宠物列表,并尝试为每只宠物添加照片。我的列表还在运行,但我以前从未在c#中使用过图像,所以我有点卡住了

到目前为止,我已经有了从超级宠物类继承的品种类(虎斑猫、暹罗猫、哈士奇猫和奇瓦瓦猫)。以下是pet类中的属性:

字符串名称、长芯片、DateTime arrivalDate、Boolean adoptedStatus、, 图像

在我的主代码文件中,我创建了一些新宠物,然后将它们添加到宠物列表中:

Tabby newTabby1 = new Tabby("sunshine", 22222222222222222, new DateTime(2016, 2, 24), false, Image.FromFile("images/sunshine.jpg"));

Chiwawa newChi1 = new Chiwawa("tony", 33333333333333333, new DateTime(2016, 2, 24), false, Image.FromFile("images/chi.jpg"));

Siamese newsia1 = new Siamese("felix", 44444444444444444, new DateTime(2016, 3, 11), false, Image.FromFile("images/felix.jpg"));

Husky newHusk1 = new Husky("fluffs", 55555555555555555, new DateTime(2016, 2, 24), false, Image.FromFile("images/husky.jpg"));


List<Pet> list = new List<Pet>();
            list.Add(newTabby1);
            list.Add(newChi1);
            list.Add(newsia1);
            StringBuilder builder = new StringBuilder();
            foreach (var item in list)
            {
                Console.WriteLine("list item " + item.Chip );
                builder.Append(item.name + " " + item.Chip + " " + item.arrivalDate + " status" + item.adoptedStatus).Append("\n");


            }
            string result = builder.ToString(); // Get string from StringBuilder
            petList.Text = result;
如果映像文件夹位于bin/Debug或bin/Release文件中(取决于您的配置),则需要使用/add Application.StartupPath属性来引用应用程序的工作目录(即bin/Debug或bin/Release,取决于您的配置)


你的电话号码是多少?您想如何显示图像?

如何在表单上显示列表?@PepitoSh我用petList显示它。text请发布一个,这意味着您还应该包括您的
Pet
类和用于显示宠物的代码。目前,我们甚至不知道您正在使用哪个GUI工具包(即WinForms或WPF)。代码显示bing已导入(成功?)的图像。问题是,我如何理解它,是如何显示所说的图像(需要什么代码声明)。我的答案来自于问题声明:“我也不确定我在创建宠物时是否正确地放入了照片。例如:”这仍然只是部分回答。。可能更适合作为注释,直到OP用表单示例更新问题,以便给出完整的答案。是的,我猜,但在任何情况下,如果他一开始在引用其图像的位置时遇到困难,他可能只想尝试使用所述属性,我猜,该位置在应用程序/可执行文件文件夹中。
 public abstract class Pet
    {

        #region Fields
        protected long chip;
        protected DateTime ArrivalDate;
        public string name;
        protected bool AdoptedStatus;
        public static int petCount = 0;
        public Image image;

        #endregion End of Fields

        #region Constructors 
        public Pet()
        {
            chip = 0;
            AdoptedStatus = false;
            petCount++;
        }
        public Pet(string name, long chip, DateTime arrivalDate, Boolean adoptedStatus, Image image)
        {
            this.chip = chip;
            ArrivalDate = arrivalDate;
            AdoptedStatus = adoptedStatus;
            this.name = name;
            petCount++;
            this.image = image;
        }
        #endregion End of Constructors

        #region Properties



        public int PetCount
        {
            get
            {
                return petCount;
            }
        }
        public long Chip
        {
            get
            {
                return chip;
            }
            set
            {
                if (value > 0)
                    chip = value;
                else
                    chip = 0;
            }
        }

        public DateTime arrivalDate { get; set; }



        public Boolean adoptedStatus { get; set; }



        #endregion End Properties

        #region Methods

        public bool UpdateStatus() => adoptedStatus = true;
        public int UpdateInventory() => petCount = petCount - 1;
        public abstract void Noise();

        // public override string ToString()
        // {
        //     return $"{Model}\n MRSP:${Mrsp}\n Vin:{Vin}\n Delivered:{DeliveryDate}\n Sold: {soldStatus}\n";
        //  }
        #endregion End of Methods
    }
string primaryDir = Application.StartupPath + "/images";