【问题标题】:c# read rgb from A1R5G5B5 image typec# 从 A1R5G5B5 图像类型中读取 rgb
【发布时间】:2011-07-15 07:11:39
【问题描述】:

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

【问题讨论】:

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

标签: c# byte rgb


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-25
    • 2013-07-10
    • 2011-06-12
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    相关资源
    最近更新 更多