比较来自世界各地的卖家的域名和 IT 服务价格

如何在存储类中拥有属性 picture/image?

我希望我类英雄中的属性有一个肖像属性 / 她的形象。 在这种情况下,我应该使用什么类型的对象?


public class Hero
{
public string Name { get; set; }
public string HeroType { get; set; }
public int StartingHP { get; set; }
public int StartingMana { get; set; }
public Dictionary<string, string=""> Spells { get; set; }
public Dictionary<string, string=""> Items { get; set; }
}


</string,></string,>
已邀请:

快网

赞同来自:

在 WPF 你必须使用这个类
ImageSource

, 例如:


public class Hero {
public string Name { get; set; }
public string HeroType { get; set; }
public int StartingHP { get; set; }
public int StartingMana { get; set; }
public Dictionary<string, string=""> Spells { get; set; }
public Dictionary<string, string=""> Items { get; set; }
public ImageSource Portrait { get; set; }
}


您可以从这样的文件中读取图像:


myHero.Portrait = new BitmapImage/new Uri/filePath, UriKind.Absolute//;


你可以使用这个类
Image

的 System.Drawing.dll. 例如:


public class Hero {
public string Name { get; set; }
public string HeroType { get; set; }
public int StartingHP { get; set; }
public int StartingMana { get; set; }
public Dictionary<string, string=""> Spells { get; set; }
public Dictionary<string, string=""> Items { get; set; }
public Image Portrait { get; set; }
}


上传图像,呼叫
Image.FromFile/path/

. 如果您在流中有图像 /例如,来自数据库或Web服务/, 你可以打电话
Image.FromStream/stream/

.

如果在编译期间有所有图像,则可以将它们放在文件中 ResX; 您可以从设计人员创建的代码文件中获取图像,例如:
myHero.Portrait = SomeResXFile.Portrait1

.
</string,></string,></string,></string,>

莫问

赞同来自:

谨防内存,其他一些答案可以领导。 这是数据对象,右,不是对象 UI?

您是否确定您将始终需要对象生命的图像? 我会担心吃记忆。 可能最好只保存资源标识符 /文件的路径,资源文件中的名称等。/ 并仅在图形字段中传输此信息。 我不会建议抓住整体的整体形象。 至少在 winforms /我不知道怎么样 WPF/ 垃圾收集器在清洁图像方面不太好。 他只认为它们是一个链接 /这是一个整数/, 因为 rest 图像主要是一个非托管对象,并相信它不是收集的高优先级。 与此同时,他可以咀嚼兆字节。 我们在以前的项目中烧了一下。

要回复问题请先登录注册