【发布时间】:2011-02-24 15:12:10
【问题描述】:
获得与System.Drawing.Imaging.PixelFormat 等效的System.Windows.Media.PixelFormats 值的最佳方法是什么?
【问题讨论】:
获得与System.Drawing.Imaging.PixelFormat 等效的System.Windows.Media.PixelFormats 值的最佳方法是什么?
【问题讨论】:
这是一种转换方式,所以让我们从这里开始,看看是否有人能超越这个可怕的装置。它们相互映射得很好,因此编写 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)。
这个话题已经很老了,但这就是我在假设两个枚举具有所有相同的字符串值的情况下解决这个问题的方法。
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;
}
【讨论】:
"Format32bppArbg",Media-PixelFormat 名称类似于"Bgra32",因此所呈现的转换是不可能的。