color palette in oxyimage

Oystein Bjorke 10 jaar geleden 0
This discussion was imported from CodePlex

BlindMonk wrote at 2013-08-19 15:34:

I am not sure how to apply color palette in an oxyimage.

I made a byte array by scaling my array in [0,255], and then I want to apply gray(255) scaling on the data. but image decoder throws error, I am pasting my code below. Please help.
            var pallet = new OxyColor[255];
            OxyPalettes.Gray(255).Colors.CopyTo(pallet, 0);
            im = OxyImage.FromIndexed8(wid, ht, bPlot,  pallet, 96);
here, wid is width( say 100), ht is height (say 100), bPlot is byte[] (size 100x100). But then I get error in image decoder which says:
The image decoder cannot decode the image. The image might be corrupted.
Thanks a lot.

objo wrote at 2013-08-22 21:49:

Maybe the palette should have 256 entries?

The following should work:
        [Test]
        public void Discussion453825()
        {
            var data = new byte[100 * 100];
            int k = 0;
            for (int i = 0; i < 100; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                    data[i + (j * 100)] = (byte)(k++ % 256);
                }
            }

            var palette = OxyPalettes.Gray(256).Colors.ToArray();
            var im = OxyImage.FromIndexed8(100, 100, data, palette);
            File.WriteAllBytes("Discussion453825.bmp", im.GetData());
        }
I have added this as a unit test.

BlindMonk wrote at 2013-08-27 17:39:

that worked, but I change 100 to 199, it doesnt!!
            var data = new byte[199 * 199];
            int k = 0;
            for (int i = 0; i < 199; i++)
            {
                for (int j = 0; j < 199; j++)
                {
                    data[i + (j * 199)] = (byte)(k++ % 256);
                }
            }

            var palette1 = OxyPalettes.Gray(256).Colors.ToArray();
            var im1 = OxyImage.FromIndexed8(199, 199, data, palette1);

objo wrote at 2013-08-27 23:09:

Thank you - this was a bug. I have corrected it, I think (DWORD aligning rows in the BITMAP pixel data section)