views:

232

answers:

2

Hi,

I am trying to render an Image object in memory with the dimensions 1x16. This image is used as a tiled background. The gradient itself should have 3 colors in a non-linear fashion.

Pixel 1 to 6: Gradient Color 1 to Color 2

Pixel 7 to 16: Gradient Color 3 to Color 4

A: 

You could use the GradientFill function.

For a custom solution, see if this article can help.

kgiannakakis
thanks for the hint. I was more looking for a managed GDI (preferable) C# solution.
Stefan Koell
+1  A: 

I just found out myself how to do it. I was expecting an answer like this:

        Bitmap bmp = new Bitmap(1, 16);
        Graphics g = Graphics.FromImage(bmp);

        System.Drawing.Drawing2D.LinearGradientBrush b1 =
            new System.Drawing.Drawing2D.LinearGradientBrush(
                new Rectangle(0, 0, 1, 6),
                Color1,
                Color2,
                System.Drawing.Drawing2D.LinearGradientMode.Vertical);

        System.Drawing.Drawing2D.LinearGradientBrush b2 =
            new System.Drawing.Drawing2D.LinearGradientBrush(
                new Rectangle(0, 7, 1, 16),
                Color3,
                Color4,
                System.Drawing.Drawing2D.LinearGradientMode.Vertical);

        g.FillRectangle(b1, new Rectangle(0, 0, 1, 6));
        g.FillRectangle(b2, new Rectangle(0, 7, 1, 16));
        g.Dispose();

The Bitmap bmp has now the 2 gradient.

Stefan Koell