调色板位图 - 分形颜色循环

时间:2012-10-18 08:45:11

标签: c# winforms bitmap fractals pallet

我正在尝试在C#窗体上使用现有的Bitmap图像,这是一个呈现的MandleBrot Fractal。我想实现色彩循环。必须使用托盘图像来完成。这是我的代码,我现在被困了几天,无法让它工作。代码必须在timer方法中。

 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();
    }

原始图像称为位图,调色板需要为bitmap2。 感谢

2 个答案:

答案 0 :(得分:1)

another answer与我的结合:

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);
}

答案 1 :(得分:0)

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;