转换GDI+;PixelFormat到WPF PixelFormat

转换GDI+;PixelFormat到WPF PixelFormat,wpf,gdi+,Wpf,Gdi+,获得相当于a的值的最佳方法是什么?这是一种转换方法,因此让我们从这里开始,看看是否有人能超越这个令人毛骨悚然的装置。它们很好地相互映射,因此编写开关用例应该相当容易 private static System.Windows.Media.PixelFormat ConvertPixelFormat(System.Drawing.Imaging.PixelFormat sourceFormat) { switch (sourceFormat) { case Syst

获得相当于a的值的最佳方法是什么?

这是一种转换方法,因此让我们从这里开始,看看是否有人能超越这个令人毛骨悚然的装置。它们很好地相互映射,因此编写开关用例应该相当容易

private static System.Windows.Media.PixelFormat ConvertPixelFormat(System.Drawing.Imaging.PixelFormat sourceFormat)
{
    switch (sourceFormat)
    {
        case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
            return PixelFormats.Bgr24;

        case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
            return PixelFormats.Bgra32;

        case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
            return PixelFormats.Bgr32;

        // .. as many as you need...
    }
    return new System.Windows.Media.PixelFormat();
}

这个主题非常古老,但我就是这样解决这个问题的,假设两个枚举都有相同的字符串值

private static System.Windows.Media.PixelFormat ConvertPixelFormat
(System.Drawing.Imaging.PixelFormat sourceFormat)
{
System.Windows.Media.PixelFormat PixelFormat=(System.Windows.Media.PixelFormat)
Parse(typeof(System.Windows.Media.PixelFormat)、sourceFormat.ToString());
返回像素格式;
}

谢谢。我最后做了类似的事情。要确保所有类似命名的格式实际上都是完全等效的,这很棘手。
System.Drawing.Imaging.PixelFormat.Format16bppArgb1555
如果我没有遗漏某些内容,似乎就没有等效的格式。但是这种格式看起来很模糊(每个5位RGB+1位alpha)。如果不支持某些格式,可能返回值应该是可选的/可为空的。如果找不到相应的格式,调用方必须决定是抛出还是执行其他操作。我不相信这是真的,因为System.Windows.Media.PixelFormat是一个结构,而不是枚举。这是绝对错误的。这段代码所做的唯一一件事就是使用Imaging PixelFormat enum对象的名称,并尝试直接将其作为媒体PixelFormat类型进行enum解析,这是不可能的:enum解析将不起作用,因为传入第一个参数的类型不是enum类型。如果这可以以任何方式工作,那么它只能以另一种方式工作,从媒体(struct)到图像(enum)。但是,即使如此,成像像素格式名称类似于
“Format32bppArbg”
,而媒体像素格式名称类似于
“Bgra32”
,因此所呈现的转换是不可能的。