ColorBlend problem - black bar at the end

I want to create a linear gradient with 7-step colors and a custom size - from black, blue, cyan, green, yellow, red to white. My problem is that the final bitmap has a black bar on the right side. Anyone have an idea what's the matter?

    public static List<Color> interpolateColorScheme(int size)
    {
        // create result list with for interpolated colors
        List<Color> colorList = new List<Color>();
        // use Bitmap and Graphics from bitmap
        using (Bitmap bmp = new Bitmap(size, 200))
        using (Graphics G = Graphics.FromImage(bmp))
        {
            // create empty rectangle canvas
            Rectangle rect = new Rectangle(Point.Empty, bmp.Size);
            // use LinearGradientBrush class for gradient computation
            LinearGradientBrush brush = new LinearGradientBrush
                                    (rect, Color.Empty, Color.Empty, 0, false);
            // setup ColorBlend object
            ColorBlend colorBlend = new ColorBlend();
            colorBlend.Positions = new float[7];
            colorBlend.Positions[0] = 0;
            colorBlend.Positions[1] = 1 / 6f;
            colorBlend.Positions[2] = 2 / 6f;
            colorBlend.Positions[3] = 3 / 6f;
            colorBlend.Positions[4] = 4 / 6f;
            colorBlend.Positions[5] = 5 / 6f;
            colorBlend.Positions[6] = 1;
            // blend colors and copy them to result color list
            colorBlend.Colors = new Color[7];
            colorBlend.Colors[0] = Color.Black;
            colorBlend.Colors[1] = Color.Blue;
            colorBlend.Colors[2] = Color.Cyan;
            colorBlend.Colors[3] = Color.Green;
            colorBlend.Colors[4] = Color.Yellow;
            colorBlend.Colors[5] = Color.Red;
            colorBlend.Colors[6] = Color.White;
            brush.InterpolationColors = colorBlend;
            G.FillRectangle(brush, rect);
            bmp.Save("gradient_debug_image_sarcus.png", ImageFormat.Png);
            for (int i = 0; i < size; i++) colorList.Add(bmp.GetPixel(i, 0));
            brush.Dispose();
        }

        // return interpolated colors
        return colorList;
    }

Here is my gradient: enter image description here

+4
source share
1 answer

I took your code and tried each size from 2 to ushort.MaxValue, generating a gradient and scanning from the right edge to determine how many black pixels there were.

. , , , . 2140 . , .

(http://www.pcreview.co.uk/threads/error-on-lineargradientbrush.2165794/). , ,

  • , ,
  • WrapMode.TileFlipX.

, , , 1 ; 127 ( ). , , , ( ), . (size + Math.Ceiling(size / 512.0)) / size, , .

, brush.WrapMode = WrapMode.TileFlipX , () , . , , . , , .

+3

All Articles