Question

I am trying to use an existing Bitmap image on a C# windows form which is a rendered MandleBrot Fractal. I want to implement colour cycling. It must be done with with the use of a pallete image. Here is my code I am stuck for days now and can't get it to work. The code must be inside the timer method.

 private void timer1_Tick(object sender, EventArgs e)
    {
                   Bitmap bitmap2 = new Bitmap(640, 480,PixelFormat.Format8bppIndexed);

        ColorPalette palette = bitmap2.Palette;

        for (int i = 0; i < 256; i += 3)
        {
            Color b = new Color(); 
            b = Color.FromArgb(i);
            bitmap2.Palette.Entries.SetValue(b, i);
           //b = Color.FromArgb(palette[i], palette[i + 1], palette[i + 2]);
           // bitmap.Palette.Entries.SetValue(b, i);
            //bitmap.Palette = palette;
        } 

        mandelbrot();
    }

The original image is called bitmap and the palette needs to be bitmap2. Thanks

Was it helpful?

Solution

ColorPalette palette = originalBitmap.Palette;
Color first = palette.Entries[0];
for (int i = 0; i < (palette.Entries.Length - 1); i++)
{
    palette.Entries[i] = palette.Entries[i + 1];
}
palette.Entries[(palette.Entries.Length - 1)] = first;
originalBitmap.Palette = palette;

OTHER TIPS

Combining another answer with mine:

var bitmap = new Bitmap(width, height, width, PixelFormat.Format8bppIndexed);

ColorPalette palette = bitmap.Palette;
palette.Entries[0] = Color.Black;
for (int i = 1; i < 256; i++)
{
    // set to whatever colour here...
    palette.Entries[i] = FromHsv(360.0*i/256, 1, 1);
}
bitmap.Palette = palette;


public Color FromHsv(double hue, double saturation, double value)
{
   int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
   double f = hue / 60 - Math.Floor(hue / 60);
   value = value * 255;
   int v = Convert.ToInt32(value);
   int p = Convert.ToInt32(value * (1 - saturation));
   int q = Convert.ToInt32(value * (1 - f * saturation));
   int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));

   if (hi == 0)
       return Color.FromArgb(255, v, t, p);
   else if (hi == 1)
       return Color.FromArgb(255, q, v, p);
   else if (hi == 2)
       return Color.FromArgb(255, p, v, t);
   else if (hi == 3)
       return Color.FromArgb(255, p, q, v);
   else if (hi == 4)
       return Color.FromArgb(255, t, p, v);
   else
       return Color.FromArgb(255, v, p, q);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top