【问题标题】:Get Image file path from Image class type从 Image 类类型中获取 Image 文件路径
【发布时间】:2013-04-13 01:53:10
【问题描述】:

我有一个关于 C# Image class type 的问题。考虑以下语句

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

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

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

【问题讨论】:

    标签: c# windows image


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      试试下面的

       img.ImageUrl
      

      【讨论】:

      • 我没有找到那个属性。我正在使用 Windows C# system.drawing.Image 类
      猜你喜欢
      • 2015-10-08
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 2014-07-24
      • 2011-08-12
      • 2015-07-05
      • 2016-02-15
      相关资源
      最近更新 更多