0

I am making a custom color dialog. It will look like this Color dialog Note at the red rectangle, I draw this hue slide on the mouse_move event of the rainbow at the bottom. My problem is the drawing speed. It is a bit slow. Is there another way to draw this panel? Here is my code:

    // picColorRuler is the rainbow below
    // picColorArea is the panel with red border in the attached image


    // Change the target location.
    private void picColorRuler_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            rulerColorTarget.Left = e.X;
            if (rulerColorTarget.Left < 0) rulerColorTarget.Left = 0;
            if (rulerColorTarget.Left > picColorRuler.ClientSize.Width - 1)
            {
                rulerColorTarget.Left = picColorRuler.ClientSize.Width - 1;
            }
            picColorRuler.Refresh();
        }
    }

    // Painting the rainbow.
    private void picColorRuler_Paint(object sender, PaintEventArgs e)
    {
        // Draw the target.
        rulerColorTarget.Color = rulerImg.GetPixel(rulerColorTarget.Left, rulerColorTarget.Top);
        rulerColorTarget.Draw(e.Graphics);
        // Change the picColorArea's colors.
        PaintPicColorArea();
    }

    private void PaintPicColorArea()
    {
        var width = picColorArea.ClientSize.Width;
        var height = picColorArea.ClientSize.Height;
        var satSeed = 1f / width;
        var lightSeed = 1f / height;
        var sat = 0f;
        for (var x = 0; x < width; ++x)
        {
            if (x == width - 1) sat = 1;
            var light = 0f;
            for (var y = 0; y < height; ++y)
            {
                if (y == height - 1) light = 1;
                var sc = Util.Hsl2Rgb(rulerColorTarget.Color.GetHue(), sat, light);
                areaImg.SetPixel(x, y, sc);
                light += lightSeed;
            }
            sat += satSeed;
        }
        picColorArea.BackgroundImage = null;
        picColorArea.BackgroundImage = areaImg;
    }
4

1 回答 1

0

Try to avoid getting & setting pixels on a bitmap image, it's notoriously slow.

I personnally used a FastBitmap but there are other ways to do it: Bitmap.SetPixel(x,y, Color) too slow .

于 2013-07-22T11:37:28.627 回答