从Image类类型获取图像文件路径

时间:2013-04-12 10:03:21

标签: c# windows image

我对C# Image class type有疑问。请考虑以下声明

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

问题:如何从img变量获取图像文件路径?

猜测: 我不认为我们可以有图像路径,因为如果我们看到方法LoadFromFile,它会从物理文件路径读取图像并将其逐字节加载到内存中。一旦加载它将在img对象中。 img对象现在将独立于磁盘上的Image物理文件。

2 个答案:

答案 0 :(得分:2)

您的猜测是正确的,图像对象在加载到内存后没有与文件的连接。您需要将路径存储在单独的变量中,或者执行以下操作:

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

答案 1 :(得分:0)

尝试使用以下

 img.ImageUrl
相关问题