c#从A1R5G5B5图像类型读取rgb

c#从A1R5G5B5图像类型读取rgb,c#,byte,rgb,C#,Byte,Rgb,在c#中,我需要将2个字节(16位)从标准0-255值转换为A1R5G5B5类型图像的像素(因此1位alpha,5位红色,5位绿色,5位蓝色) 提前感谢这是一个快速而肮脏的解决方案,但它应该适合您 using System.Drawing; class ShortColor { public bool Alpha { get; set; } public byte Red { get; set; } public byte Green { get; set; }

在c#中,我需要将2个字节(16位)从标准0-255值转换为A1R5G5B5类型图像的像素(因此1位alpha,5位红色,5位绿色,5位蓝色)
提前感谢

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

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

听起来您不太可能只想转换2个字节。A1R5G5B5格式只有2个字节。。。例如,如果你从photoshop保存一个2或3色平面的TGA,然后压缩图像,TGA将以A1R5G5B5格式保存。。。