【问题标题】:C# .net: add image to list and show on formC# .net:将图像添加到列表并显示在表单上
【发布时间】:2018-06-05 06:03:59
【问题描述】:

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

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

字符串名称,长芯片,DateTime到达日期,布尔值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;

我的项目文件中有一个 images 文件夹,其中包含 jpg 格式的宠物照片。我不知道如何让照片显示在我的列表中,旁边是名称等其他属性。我也不确定我在创建宠物时是否正确放置了照片 ex: (Image.FromFile("images/husky.jpg")。

这也是我的宠物班:

 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
    }

【问题讨论】:

  • 如何在表单上显示列表?
  • @PepitoSh 我用 petList.Text 显示它
  • 请发布MVCE,这意味着您还应该包括您的Pet 课程和用于显示您的宠物的代码。目前我们甚至不知道您使用的是哪个 GUI-Toolkit(即 WinForms 或 WPF)。

标签: c# .net


【解决方案1】:

如果 images 文件夹在您的 bin/Debug 或 bin/Release 文件中(取决于您的配置),那么您需要使用/添加 Application.StartupPath 属性来引用您的工作目录应用程序(即 Bin/Debug 或 Bin/Release,具体取决于您的配置)。

string primaryDir = Application.StartupPath + "/images";

【讨论】:

  • 据我了解,这不是问题。该代码显示了已经导入的图像(成功吗?)。问题,我是如何理解的,是如何显示所述图像(需要什么代码语句)。
  • 我的回答源于问题陈述:“我也不确定我在创建宠物时是否正确放置了照片:
  • 这仍然只是部分回复。在 OP 用表单示例更新问题之前,作为评论可能更合适,因此可以给出完整的答案..
  • 是的,我想,但无论如何,如果他一开始在引用他的图像的位置时被绊倒了,他可能只是想尝试使用上述属性,在我的猜测中,在应用程序/可执行文件夹。
【解决方案2】:

你的前台代码是什么?你想如何显示图像?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多