0

I know you must here the IndexOutOfBoundsException loads, and I wouldn't normally post stuff about it, but I have just come across it whilst trying to parse an array as a palette using the following code. It throws the exception when i = 0 and palette.Length = 768, I can't see why and I'm sure this code worked before:

        ColorPalette palette1 = bmp.Palette;

        for (int i = 0; i < palette.Length; i += 3)
        {
            if (i != 0)
            {
                Color b = Color.FromArgb(255, palette[i], palette[i + 1], palette[i + 2]);
                palette1.Entries[i/3] = b;
            }

            else
            {
                Color b = Color.FromArgb(255, palette[i], palette[i + 1], palette[i + 2]);
                palette1.Entries[i] = b;
            }
        }

        bmp.Palette = palette1;

The following code DOES work, but uses a smaller palette in a separate function:

        ColorPalette palette1 = bmp.Palette;

        for (int i = 0; i < 48; i += 3)
        {
            if (i != 0)
            {
                Color b = Color.FromArgb(255, palette[i], palette[i + 1], palette[i + 2]);
                palette1.Entries[i / 3] = b;
            }

            else
            {
                Color b = Color.FromArgb(255, palette[i], palette[i + 1], palette[i + 2]);
                palette1.Entries[i] = b;
            }
        }

        bmp.Palette = palette1;
user646265
  • 1,011
  • 3
  • 14
  • 32

1 Answers1

1

Your loop variable i is bounded by palette.Length, but you're trying to assign to palette1. There's no guarantee that palette1 has the same length as palette, and my guess is that it does not -- hence your problem.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
  • I've tried this, but palette.Entries.Length is zero and now can't be added to the bitmap again. Anyway, that doesn't explain why it worked for the last loop. – user646265 Apr 14 '11 at 15:58
  • @user: try adding a line to explicitly check that `(palette.Length / 3) == palette1.Entries.Length`, and see if it fails. Your second entry would succeed as long as there are at least 48 entries in `palette1.Entries`, which is evidently true. – JSBձոգչ Apr 14 '11 at 16:01
  • It's not even entering the loop now, instead it's throwing an error (parameter is not valid) when trying to re-assign the palette with 'bmp.Palette = palette1;'. It seems that palette1's length is zero, so it won't loop if 256 doesn't equal 0. – user646265 Apr 14 '11 at 16:08
  • If the existing bitmap palette has length 0, then you can't modify it in place. You'll have to create a new bitmap with a new palette and copy the data from the old bitmap over, then reassign the palette entries. – JSBձոգչ Apr 14 '11 at 16:13
  • I have copied 'bmp' to a new bitmap 'bmp2' and then used 'bmp2.Palette', but I still get the same issue. Why would this be different from the other loop, it's the same code, but with a different amount of data? – user646265 Apr 14 '11 at 16:29
  • I'm guessing, because you're not really giving me the information I need, but I'm guessing that you just used `bmp2 = new Bitmap(bmp)`. That won't work, since it creates a bitmap that's identical to the old one and has the same problem. You need to use the constructor `System.Drawing.Bitmap(Int32, Int32, PixelFormat)` and specify `PixelFormat.Format8bppIndexed` as the last argument, then copy the actual pixel array, *then* modify the color table. – JSBձոգչ Apr 14 '11 at 16:38
  • Alright, found the issue: I was calling this function from a seperate class and when i called it to create an 8 bit per pixel file but I called px (the pixel format) as a 24bppRGB. 24-bit files don't have palettes, so the palette index was empty, which explains why it worked on the 4bpp example. Oops, still thanks for the help! – user646265 Apr 15 '11 at 16:18