4

I have a person class as below, with an image as an attribute. I'm trying to figure out how to create an instance of the person class in my program class, and set the image of the object to a filepath, e.g. C:\Users\Documents\picture.jpg. How would I go about this?

 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;
    }
 }
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
bookthief
  • 2,443
  • 8
  • 41
  • 54
  • 2
    `myImage` is of type `Image` and not `String`, so where do you want to store the file-path? Or do you want change the type of this property accordingly? – Tim Schmelter Jul 03 '13 at 14:18

4 Answers4

1

Using your parameter-less constructor, will be as follows:

Person person = new Person();
Image newImage = Image.FromFile(@"C:\Users\Documents\picture.jpg");
person.myImage = newImage;

Although using the other constructor should be the preferred approach

Liel
  • 2,407
  • 4
  • 20
  • 39
  • Thanks, but do I need to import any namespaces in the program class to do this? I have System.Drawing, but there is no FromFile option listed after Image. – bookthief Jul 03 '13 at 14:20
  • `FromFile` is part of [Image Class](http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx). Do you have any other classes in your project that is named `Image`? – Liel Jul 03 '13 at 14:21
1

One option would be this:

public Person(string firstName, string lastName, string imagePath)
{
    ...
    this.myImage = Image.FromFile(imagePath);
}
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
1

Try it like this:

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

And instantiate like this:

Person p = new Person("John","Doe",@"C:\Users\Documents\picture.jpg");
Igor
  • 15,833
  • 1
  • 27
  • 32
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
1

Everyone else has already suggested Image.FromFile. You should be aware that this will lock the file, which might cause problems later. More reading here:

Why does Image.FromFile keep a file handle open sometimes?

Consider using the Image.FromStream method instead. Here's an example method that takes a path and returns an image:

private static Image GetImage(string path)
{
    Image image;
    using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
    {
        image = Image.FromStream(fs);
    }
    return image;
}

The value in this approach is that you control when you open and close your file handle.

Community
  • 1
  • 1
Curtis Rutland
  • 776
  • 4
  • 12