0

是否可以将图像设置为用户定义类中对象的属性?就像在下面的类中一样-我可以将每个人的图像作为属性并将其包含在构造函数中吗?我已经对此进行了很多谷歌搜索,但没有找到任何东西,我觉得我错过了一些非常明显的东西,但我似乎无法弄清楚如何导入一个图像类——完全是初学者。

 public class Person
 {
    String fName, lName;

    public Person()
    {
    }

    public Person(string firstName, string lastName)
    {
        this.fName = firstName;
        this.lName = lastName;
    }
4

5 回答 5

2

如果你真的想把一个图像像一个Attribute,你可能会考虑
使用base64图像的 rapresentation 像那个属性的字符串值。

从来没有做过这样的事情,所以不知道这是否有一些长度限制 CLR。你应该自己试试。

即使这看起来像是“很酷的主意”,我还是建议您只拥有外部资源,并且

  • 或具有类似属性的资源名称
  • 或链接到 IO 数据(文件、数据库字段..)。
于 2013-07-03T10:15:33.500 回答
1

是的:

 public class Person
 {
    public string fName { get; set; }
    public string lName { get; set; }
    public Image image { get; set; }

    public Person()
    {
    }

    public Person(string firstName, string lastName, Image image)
    {
        this.fName = firstName;
        this.lName = lastName;
        this.image = image;
    }
 }
于 2013-07-03T10:16:04.407 回答
1

这取决于你需要什么。您可以随时添加带有图像 URL 的字符串属性,并在浏览器、表单或任何其他需要显示它的应用程序中使用它。我建议您保存在某个位置并知道该位置的确切 URL。URL 应该是公开的,任何需要阅读的人都可以访问。

编辑:错过的代码

public class Person
 {
    String fName, lName, imageUrl;

    public Person()
    {
    }

    public Person(string firstName, string lastName, string imageUrl)
    {
        this.fName = firstName;
        this.lName = lastName;
        this.imageUrl = imageUrl;
    }
}
于 2013-07-03T10:16:16.797 回答
1

当然这是可能的,您的代码如下所示:

public class Person
{
    String fName, lName;
    Image pImage;

    public Person()
    {
    }

    public Person(string firstName, string lastName, Image img)
    {
        this.fName = firstName;
        this.lName = lastName;
        this.pImage = img;
    }
}
于 2013-07-03T10:17:25.950 回答
1

是的你可以。

源文件顶部添加

using System.Drawing;

在你的课堂上添加

Person
{
   Image myImage;

在您的构造函数中,只需包含一个 Image。图像的工作方式与字符串非常相似。

public Person(string firstName, string lastName, Image profilePic)
{
  // your stuff
   this.myImage = profilePic;
}
于 2013-07-03T10:17:32.443 回答