【问题标题】:Converting GDI+ PixelFormat to WPF PixelFormat将 GDI+ PixelFormat 转换为 WPF PixelFormat
【发布时间】:2011-02-24 15:12:10
【问题描述】:

获得与System.Drawing.Imaging.PixelFormat 等效的System.Windows.Media.PixelFormats 值的最佳方法是什么?

【问题讨论】:

    标签: wpf gdi+


    【解决方案1】:

    这是一种转换方式,所以让我们从这里开始,看看是否有人能超越这个可怕的装置。它们相互映射得很好,因此编写 switch case 应该相当容易。

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

    【讨论】:

    • 谢谢。我最终做了类似的事情。要确保所有类似命名的格式实际上是完全等价的,这很棘手。
    • System.Drawing.Imaging.PixelFormat.Format16bppArgb1555 如果我没有错过任何东西,似乎没有等价物。但无论如何,这种格式似乎相当模糊(每个 5 位 RGB + 1 位 alpha)。
    • 如果有些不支持,也许返回值应该是可选的/可为空的。如果没有找到相应的格式,调用者必须决定是抛出还是做其他事情。
    【解决方案2】:

    这个话题已经很老了,但这就是我在假设两个枚举具有所有相同的字符串值的情况下解决这个问题的方法。

    private static System.Windows.Media.PixelFormat ConvertPixelFormat (System.Drawing.Imaging.PixelFormat sourceFormat) { System.Windows.Media.PixelFormat pixelFormat = (System.Windows.Media.PixelFormat) Enum.Parse(typeof(System.Windows.Media.PixelFormat), sourceFormat.ToString());
    return pixelFormat; }

    【讨论】:

    • 我不相信这是真的,因为 System.Windows.Media.PixelFormat 是一个结构,而不是一个枚举。
    • 这绝对是错误的。这段代码唯一要做的就是获取 Imaging-PixelFormat 枚举对象的 name 并将尝试直接 Enum-Parse 它作为 Media-PixelFormat 类型,这是不可能的:Enum-Parse 将不起作用,因为传入第一个参数的类型不是枚举类型。如果这可以以任何方式起作用,那么它只能以相反的方式起作用,从 Media (struct) 到 Imaginig (enum)。但是,即便如此,Imaging-PixelFormat 名称类似于"Format32bppArbg",Media-PixelFormat 名称类似于"Bgra32",因此所呈现的转换是不可能的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 2016-09-07
    • 1970-01-01
    • 2023-03-08
    • 2015-02-23
    • 1970-01-01
    • 2019-09-30
    相关资源
    最近更新 更多