RGBA图像格式(由Scintilla使用)

时间:2013-03-12 16:10:40

标签: c# rgba scintilla

我正在使用Scintilla(不是ScintillaNET),我正在尝试设置自动完成列表中使用的图像。 Scintilla需要一串RGBA格式的字节/像素。

Scintilla对此RGBA格式的描述:Quote from documentation

  

RGBA格式允许半透明,每个格式具有alpha值   像素。它比XPM更简单,更强大。

     

数据是以像素开始的4字节像素值序列   对于顶行,首先是最左边的像素,然后继续   后续行的像素。线之间没有间隙   对齐原因。

     

每个像素按顺序由红色字节,绿色字节,蓝色组成   字节和字母字节。颜色字节不会被预乘   alpha值。也就是说,25%不透明的全红色像素将是   [FF,00,00,3F]

我希望我误解了上面解释的格式,文档不是很清楚。

我的转换方法:

我编写了这个函数来将PNG转换为这个字节串:

public static string ConvertFromPNG(Bitmap PNG)
{
    string rgba = "";

    int pixWidth = PNG.Width;
    int pixHeight = PNG.Height;

    for (var y = 0; y < pixHeight; y++)
    {
        for (var x = 0; x < pixWidth; x++)
        {
            Color pix = PNG.GetPixel(x, y);

            rgba += pix.R.ToString("X2") + pix.G.ToString("X2") + pix.B.ToString("X2") + pix.A.ToString("X2");
        }
    }

    return rgba;
}

结果图片:

但是Scintilla只是将图像显示为灰色框:

Autocomplete box

相关图片是Microsoft's Babel images之一Babel icon的副本。

我知道字节集是正确的,因为有趣的是,如果我在行中格式化它们并缩小它们,我可以看到图像的轮廓:

Bytes(Zoomed out in image editor)

字节生成:

00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009F000000EC0000001B00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000815B0000DF9B0000F8000000B40000002F000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001B0000007F4E0000FDC80000FFFF0000FF720000FF1D0000B50000003E00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001613000085350000FFDE0000FFFF0000FFFF0000FFFF0000FF750000FF230000C4000000240000000000000000000000000000000000000000000000000000000000000000000000000000000000000085400000EADB0000FFF90000FFFF0000FFFF0000FFFF0000FFF60000FFA70000F5000000DA00000000000000000000000000000000000000000000000000000000000000000000000000000091810000EACA0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFBB0000FF460000FF000000FF000000000000000000000000000000000000000000000000000000000000000000000000000000FF7E0000FFE80000FFFF0000FFFF0000FFFF0000FFFB0000FFB30000FF460000FF0C0000FF000000FF000000000000000000000000000000000000000000000000000000000000000000000000000000FF000000FF660000FFFF0000FFFF0000FFFF0000FFC70000FF0D0000FF590000FF7A0000FF000000FF000000000000000000000000000000000000000000000000000000000000000000000000000000FF8C0000FF7D0000FF2E0000FFFF0000FFBB0000FF1C0000FF510000FFB40000FF7B0000FF000000FF000000000000000000000000000000000000000000000000000000000000000000000000000000FFE20000FFE40000FF410000FF8B0000FF510000FF350000FFB60000FF8F0000FF3A0000EA00000091000000000000000000000000000000000000000000000000000000000000000000000000000000DA910000F5F30000FFFF0000FF0C0000FF270000FFC10000FF930000FF220000EA0000008500000000000000000000000000000000000000000000000000000000000000000000000000000000000000241D0000C4870000FFFF0000FF270000FF420000FF9E0000FF230000FF090000860000001600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003E2D0000B5610000FF120000FF1C0000FF370000FD0000007F0000001B000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002E000000B4000000F8000000DF00000081000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001B000000EC0000009F0000000000000000000000000000000000000000

2 个答案:

答案 0 :(得分:2)

您将字节格式化为十六进制值并将它们放在一个字符串中。 Scintilla只想要字节本身。

我怀疑你想创建一个字节数组而不是字符串,并直接存储pix.Rpix.Gpix.Bpix.A的值,而不是将它们格式化为字符串。

答案 1 :(得分:0)

在接受JasonD的回答之后,我已经更新了我的代码并且它正在运行。万一它可以帮助其他人,这是工作代码:

public static byte[] ConvertFromPNG(Bitmap PNG)
{
    byte[] rgbaB = new byte[4 * (PNG.Width * PNG.Height)];

    int i = 0;

    for (var y = 0; y < PNG.Height; y++)
    {
        for (var x = 0; x < PNG.Width; x++)
        {
            Color pix = PNG.GetPixel(x, y);

            rgbaB[i++] = pix.R;
            rgbaB[i++] = pix.G;
            rgbaB[i++] = pix.B;
            rgbaB[i++] = pix.A;
        }
    }

    return rgbaB;
}