0

我正在尝试在 C# windows 窗体上使用现有的位图图像,它是渲染的 MandleBrot 分形。我想实现颜色循环。必须使用调色板图像来完成。这是我的代码,我被困了好几天,无法让它工作。代码必须在 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();
    }

原始图像称为位图,调色板需要是位图2。谢谢

4

2 回答 2

1

另一个答案与我的结合起来:

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);
}
于 2012-10-18T09:44:55.143 回答
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;
于 2012-10-18T16:40:07.100 回答