C# ImageFormat to string

前端 未结 4 1694
灰色年华
灰色年华 2021-02-07 23:24

How can I obtain human readable string(i.e. image format itself) from System.Drawing.ImageFormat object?

I mean if I have ImageF

4条回答
  •  Happy的楠姐
    2021-02-07 23:33

    ImageFormat values are identified by Guid you need to create your own map of Guid -> Name

    var dict = (
        from t in typeof(ImageFormat).GetProperties()
        where t.PropertyType == typeof(ImageFormat)
        let v = (ImageFormat)t.GetValue(null, new object[0])
        select new { v.Guid, t.Name }
        ).ToDictionary(g => g.Guid, g => g.Name);
    
    string name;
    if (dict.TryGetValue(ImageFormat.Png.Guid, out name))
    {
        Console.WriteLine(name);
    }
    

提交回复
热议问题