我怎样才能在课堂上有一个属性来存储图片/图像?

时间:2021-11-19 21:33:31

I want a attribute in my Hero class to have a Portrait attribute for his/her image. What object type should I use in this case?

我想在我的Hero类中有一个属性为他/她的图像设置一个Portrait属性。在这种情况下我应该使用什么对象类型?

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; }        
}

3 个解决方案

#1


4  

In WPF, you should use the ImageSource class, like this:

在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; }
}

You can read an image from a file like this:

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

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

You can use the Image class from System.Drawing.dll. For example:

您可以使用System.Drawing.dll中的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; } 
    public Image Portrait { get; set; }
}

To load the image, call Image.FromFile(path). If you have an image in a stream (eg, from a database or web service, you can call Image.FromStream(stream).

要加载图像,请调用Image.FromFile(path)。如果流中有图像(例如,来自数据库或Web服务,则可以调用Image.FromStream(stream)。

If you have all of the images at compile time, you can put them in a ResX file; you can get an image from the designer-generated code file like this: myHero.Portrait = SomeResXFile.Portrait1.

如果您在编译时拥有所有图像,则可以将它们放在ResX文件中;您可以从设计器生成的代码文件中获取图像,如下所示:myHero.Portrait = SomeResXFile.Portrait1。

#2


3  

Beware the memory hogging some of the other answers may lead to. This is a data object right, not a UI object?

当心内存占用其他一些答案可能会导致。这是一个数据对象权限,而不是UI对象?

Do you know for a fact that you will always need the image for the life of the object? I would be concerned about eating up memory. It's probably better to just keep a resource identifier (file path, name in resource file, etc) and only pass that info on to image boxes. I wouldn't suggest holding on to the whole image. In winforms at least (don't know about WPF) the garbage collector isn't so hot at cleaning up images. It only sees them as a reference (i.e. an integer) because the rest of the image is basically an unmanaged object and thinks it's not a high priority for collection. Meanwhile, it could be chewing up megabytes. We got burned on a previous project of mine on that.

你是否知道一个事实,你将始终需要图像的对象的生命?我会担心吃掉记忆力。最好只保留资源标识符(文件路径,资源文件中的名称等),并仅将该信息传递给图像框。我不建议坚持整个形象。至少在winforms中(不了解WPF)垃圾收集器在清理图像方面不是那么热门。它只将它们视为参考(即整数),因为图像的其余部分基本上是一个非托管对象,并认为它不是收集的高优先级。与此同时,它可能正在咀嚼兆字节。我们之前的一个项目被烧毁了。

#3


0  

How about this:

这个怎么样:

System.Drawing.Image

为System.Drawing.Image

#1


4  

In WPF, you should use the ImageSource class, like this:

在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; }
}

You can read an image from a file like this:

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

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

You can use the Image class from System.Drawing.dll. For example:

您可以使用System.Drawing.dll中的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; } 
    public Image Portrait { get; set; }
}

To load the image, call Image.FromFile(path). If you have an image in a stream (eg, from a database or web service, you can call Image.FromStream(stream).

要加载图像,请调用Image.FromFile(path)。如果流中有图像(例如,来自数据库或Web服务,则可以调用Image.FromStream(stream)。

If you have all of the images at compile time, you can put them in a ResX file; you can get an image from the designer-generated code file like this: myHero.Portrait = SomeResXFile.Portrait1.

如果您在编译时拥有所有图像,则可以将它们放在ResX文件中;您可以从设计器生成的代码文件中获取图像,如下所示:myHero.Portrait = SomeResXFile.Portrait1。

#2


3  

Beware the memory hogging some of the other answers may lead to. This is a data object right, not a UI object?

当心内存占用其他一些答案可能会导致。这是一个数据对象权限,而不是UI对象?

Do you know for a fact that you will always need the image for the life of the object? I would be concerned about eating up memory. It's probably better to just keep a resource identifier (file path, name in resource file, etc) and only pass that info on to image boxes. I wouldn't suggest holding on to the whole image. In winforms at least (don't know about WPF) the garbage collector isn't so hot at cleaning up images. It only sees them as a reference (i.e. an integer) because the rest of the image is basically an unmanaged object and thinks it's not a high priority for collection. Meanwhile, it could be chewing up megabytes. We got burned on a previous project of mine on that.

你是否知道一个事实,你将始终需要图像的对象的生命?我会担心吃掉记忆力。最好只保留资源标识符(文件路径,资源文件中的名称等),并仅将该信息传递给图像框。我不建议坚持整个形象。至少在winforms中(不了解WPF)垃圾收集器在清理图像方面不是那么热门。它只将它们视为参考(即整数),因为图像的其余部分基本上是一个非托管对象,并认为它不是收集的高优先级。与此同时,它可能正在咀嚼兆字节。我们之前的一个项目被烧毁了。

#3


0  

How about this:

这个怎么样:

System.Drawing.Image

为System.Drawing.Image