2011-07-14 75 views
0

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

+0

这听起来不太可能只是想转换2个字节。 –

+0

A1R5G5B5格式只有2个字节...例如,如果您从Photoshop中保存2或3色平坦的TGA,并且您压缩图像,则tga将以A1R5G5B5格式保存... – ghiboz

回答

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

谢谢,我会尝试!! – ghiboz