You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中如何修改调色板中的特定像素(而非全部颜色)?

针对Sprite调色板模式修改特定像素的解决方案

核心原理

调色板模式的Sprite中,像素数据存储的是调色板的索引值(而非直接的RGB颜色)。所以修改特定位置的像素有两种思路:要么修改该像素对应的调色板索引,要么修改调色板中某索引的颜色(会影响所有使用该索引的像素)。

方案1:修改特定位置像素的调色板索引

这种方式只改变目标(x,y)处的像素颜色,不影响其他像素。步骤如下:

  1. 获取Sprite的像素索引数组(通常是byte[]类型,需根据Sprite类的API获取,比如sprite.Pixels或sprite.GetPixelData())
  2. 计算目标坐标对应的数组下标:下标 = y * 宽度 + x
  3. 将该下标处的字节值改为目标颜色在调色板中的索引(若目标颜色不在调色板,需先添加/替换)

示例代码:

// 定义目标坐标和颜色
int targetX = 5;
int targetY = 3;
Color targetColor = Color.FromArgb(0, 0, 255);

// 获取Sprite的基础信息
int spriteWidth = sprite.Width;
int spriteHeight = sprite.Height;
byte[] pixelIndices = sprite.Pixels; // 假设Sprite有此属性

// 计算目标像素在数组中的位置
int pixelArrayIndex = targetY * spriteWidth + targetX;

// 查找目标颜色在调色板中的索引
int colorPaletteIndex = Array.FindIndex(sprite.Palette, c => 
    c.R == targetColor.R && c.G == targetColor.G && c.B == targetColor.B);

// 若颜色不在调色板中,添加或替换(以8位调色板为例,最多256色)
if (colorPaletteIndex == -1)
{
    if (sprite.Palette.Length < 256)
    {
        // 扩展调色板并添加新颜色
        Array.Resize(ref sprite.Palette, sprite.Palette.Length + 1);
        colorPaletteIndex = sprite.Palette.Length - 1;
        sprite.Palette[colorPaletteIndex] = targetColor;
    }
    else
    {
        // 调色板已满,替换某个闲置索引(这里示例替换索引0,需根据实际调整)
        colorPaletteIndex = 0;
        sprite.Palette[colorPaletteIndex] = targetColor;
    }
}

// 修改目标像素的索引
pixelIndices[pixelArrayIndex] = (byte)colorPaletteIndex;

方案2:修改调色板中特定索引的颜色

如果希望所有使用某一调色板索引的像素统一变色,直接修改调色板对应位置的颜色即可:

// 将调色板中索引为3的颜色改为蓝色
sprite.Palette[3] = Color.FromArgb(0, 0, 255);
// 所有使用索引3的像素都会同步变为蓝色

关于Sprite转字节数组的说明

常规Bitmap转字节数组的方法不适用,需使用Sprite类自身提供的API获取像素索引数组(比如上述示例中的sprite.Pixels),这类数组直接对应调色板的索引值,修改后无需额外转换即可生效。

内容的提问来源于stack exchange,提问作者C0deM0nkey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 19:27:32