1

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

2 Answers 2

2

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.

1

You could use the GradientFill function.

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

1
  • thanks for the hint. I was more looking for a managed GDI (preferable) C# solution. May 31, 2009 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.