C#的imageformat串 [英] C# ImageFormat to string

查看:2305
本文介绍了C#的imageformat串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从的 System.Drawing.ImageFormat 的对象获得的人类可读的字符串(即图像格式本身)?

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

我的意思是,如果我有 ImageFormat.Png 是可以将其转换为?PNG串

I mean if I have ImageFormat.Png is that possible to convert it to "png" string?

编辑:我在这里看到了一些误解。这里是我的代码:

I see some misunderstanding here. Here is mine code:

Image objImage = Image.FromStream(file);

ImageFormat imFormat = objImage.RawFormat;

imFormat.ToString(); 



返回 [的imageformat:b96b3caf-0728-11d3- 9d7b-0000f81ef32e] ,但我想要的

It returns "[ImageFormat: b96b3caf-0728-11d3-9d7b-0000f81ef32e]" but I want "Png"!

推荐答案

ImageFormat.Png.ToString()返回巴...

编辑:好的,看来的ToString 只返回的imageformat 由静态属性返回实例的名称..

OK, it seems ToString returns the name only for ImageFormat instances returned by the static properties...

您可以创建一个查找字典,从该GUID获取名称:

You could create a lookup dictionary to get the name from the Guid:

private static readonly Dictionary<Guid, string> _knownImageFormats =
            (from p in typeof(ImageFormat).GetProperties(BindingFlags.Static | BindingFlags.Public)
             where p.PropertyType == typeof(ImageFormat)
             let value = (ImageFormat)p.GetValue(null, null)
             select new { Guid = value.Guid, Name = value.ToString() })
            .ToDictionary(p => p.Guid, p => p.Name);

static string GetImageFormatName(ImageFormat format)
{
    string name;
    if (_knownImageFormats.TryGetValue(format.Guid, out name))
        return name;
    return null;
}

这篇关于C#的imageformat串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆