设置对象的图像属性
本文关键字:属性 图像 对象 设置 | 更新日期: 2023-09-27 18:20:53
是否可以将图像设置为用户定义类中对象的属性?就像在下面的类中一样,我可以将每个人的图像作为属性并将其包含在构造函数中吗?我在谷歌上搜索了很多,但没有找到任何东西,我有一种感觉,我错过了一些很明显的东西,但我似乎不知道如何导入一个图像类——完全是初学者。
public class Person
{
String fName, lName;
public Person()
{
}
public Person(string firstName, string lastName)
{
this.fName = firstName;
this.lName = lastName;
}
如果您真的想放置像Attribute
这样的图像,您可以考虑
使用CCD_ 2表示图像,就像该属性的字符串值一样。
我从未做过这样的事情,所以不知道中是否有长度限制CLR
。你应该自己试试。
即使这看起来像是一个"很酷的想法",我还是建议拥有外部资源和
- 或具有类似资源名称的属性
- 或链接到IO数据(文件、DB字段..)
可以。
在源文件的顶部添加
using System.Drawing;
在你的课堂上添加
Person
{
Image myImage;
在构造函数中,只需包含一个Image。图像的工作原理与字符串非常相似。
public Person(string firstName, string lastName, Image profilePic)
{
// your stuff
this.myImage = profilePic;
}
是的:
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;
}
}
这取决于您需要什么。您可以随时添加带有图像URL的String属性,并在浏览器、窗体或任何其他需要显示它的应用程序中使用它。我的建议是,你保存在某个位置,并知道它的确切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;
}
}
当然这是可能的,您的代码看起来像:
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;
}
}