c#从A1R5G5B5图像类型读取rgb

时间:2011-07-14 21:45:43

标签: c# byte rgb

我需要在c#中转换2个字节(16位),它是标准0-255值的A1R5G5B5类型的像素(所以1位alpha,5位红色,5位绿色,5位蓝色) 提前谢谢

1 个答案:

答案 0 :(得分:2)

这是一个快速而肮脏的解决方案,但它应该适合您。

using System.Drawing;

class ShortColor
{
    public bool Alpha { get; set; }

    public byte Red   { get; set; }
    public byte Green { get; set; }
    public byte Blue  { get; set; }

    public ShortColor(short value)
    {
         this.Alpha = (value & 0x8000) > 0;

         this.Red = (byte)((value & 0x7C64) >> 10);
         this.Green = (byte)((value & 0x3E0) >> 5);
         this.Blue = (byte)((value & 0x001F));
    }

    public ShortColor(Color color)
    {
         this.Alpha = color.A != 0;

         this.Red = (byte)(color.R / 8);
         this.Green = (byte)(color.G / 8);
         this.Blue = (byte)(color.B / 8);
    }

    public static explicit operator Color(ShortColor shortColor)
    {
         return Color.FromArgb(
             shortColor.Alpha ? 255 : 0,
             shortColor.Red * 8,
             shortColor.Green * 8,
             shortColor.Blue * 8
         );
    }
}