Mendy
Mendy

Reputation: 157

Get Image file path from Image class type

I have a query regarding C# Image class type. Consider the following statement

Image img = Image.LoadFromFile(@"C:\1.jpg");

Question: How can I get the image file path from the img variable?

Guess: I dont think we can have image path because, if we see the method LoadFromFile, it reads image from physical file path and loads it into memory byte by byte. Once its loaded it will be in img object. img object now will be independent of Image physical file on the disk.

Upvotes: 1

Views: 9794

Answers (2)

TheQ
TheQ

Reputation: 7017

Your guess is correct, the image-object doesn't have a connection to the file after it's been loaded into memory. You need to store the path in a separate variable, or do something like this:

public class ImageEx
{
    public Image Image { get; set; }
    public string Filename { get; set; }

    public ImageEx(string filename)
    {
        this.Image = Image.FromFile(filename);
        this.Filename = filename;
    }
}

public class SomeClass
{
    public void SomeMethod()
    {
        ImageEx img = new ImageEx(@"C:\1.jpg");

        Debug.WriteLine("Loaded file: {0}", img.Filename);
        Debug.WriteLine("Dimensions: {0}x{1}", img.Image.Width, img.Image.Height);
    }
}

Upvotes: 3

Rajeev Kumar
Rajeev Kumar

Reputation: 4963

Try using the following

 img.ImageUrl

Upvotes: 0

Related Questions