c# - 将图像属性设置为文件路径

标签 c# .net

我有一个如下所示的人员类,其中有一个图像作为属性。我试图弄清楚如何在我的程序类中创建 person 类的实例,并将对象的图像设置为文件路径,例如C:\Users\Documents\图片.jpg。我该怎么做?

 public class Person
 {
    public string firstName { get; set; }
    public string lastName { get; set; }
    public Image myImage { get; set; }

    public Person()
    {
    }

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

最佳答案

像这样尝试:

public class Person
 {
    public string firstName { get; set; }
    public string lastName { get; set; }
    public Image myImage { get; set; }
    public Person()
    {
    }
    public Person(string firstName, string lastName, string imagePath)
    {
       this.fName = firstName;
       this.lName = lastName;
       this.myImage = Image.FromFile(imagePath);
    }
}

然后像这样实例化:

Person p = new Person("John","Doe",@"C:\Users\Documents\picture.jpg");

关于c# - 将图像属性设置为文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17450415/

相关文章:

c# - 比较 LINQ to SQL 中的两个日期

c# - 为什么 TcpListener 没有实现 IDisposable

c# - 在C#中读取datagridview每一行的单元格

c# - 在现有 Web API 中支持 ODataQueryOptions

c# - 访问本地 IIS : "This operation is not supported for a relative URI." 上托管的 WCF 服务时出错

.net - Swift 和 .Net Web 应用程序的业务类

c# - 在 C# .NET Core 的断言有效负载中使用自定义数据为第 3 方应用程序创建 SAML 断言

javascript - 从 Blazor 调用 JavaScript : how to dispose JavaScript within DotNetObjectReference?

c# - 在 C# 中将方法调用中的字符串数组初始化为参数

c# - ServiceProvider 不为 transient EF 上下文释放内存