This project has moved and is read-only. For the latest updates, please go here.

Creating an equivalent of a CSS linear gradient from top to bottom

Apr 24 at 11:50 PM
Hi Dirk,

I'm trying to figure out how to go about creating a 2-color gradient dynamically oriented from top to bottom right with manually specified color stops. The CSS equivalent would be:

background: linear-gradient(to bottom right, #000000 49%, #ffffff 50%);

Here is an image with the sample effect: https://ibb.co/kHKuO5

Any help would be appreciated - thanks for your hard work.
Apr 25 at 9:06 PM
Is your goal to create a gradient or do you just want half of the image to be white and the other half black at a 45 degrees rotation?
Apr 25 at 10:14 PM
Edited Apr 25 at 10:21 PM
Now that you mention it, I'm not sure if "gradient" is the proper word here, as I don't want the colors to fade in to each other. Essentially, it's a gradient with hard color stops that gives it the appearance of 2 colors at a 45 degree angle. The hex colors can be dynamically specified.
Apr 25 at 11:56 PM
What you could do is this:
int size = 100;

using (MagickImageCollection images = new MagickImageCollection())
{
  images.Add(new MagickImage(new MagickColor("#000000"), size * 2, size * 2));
  images.Add(new MagickImage(new MagickColor("#ffffff"), size * 2, size * 2));

  using (MagickImage image = images.AppendHorizontally())
  {
    image.Rotate(45);

    image.RePage();
    image.Crop(size, size, Gravity.Center);

    image.Write(@"d:\test.png");
  }
}
This will create an image larger than what you want rotate it and then crop it to the size that you want. Not sure if the times 2 approach will work for every situation. Not a math expert here :)
Apr 26 at 2:27 PM
This works beautifully - thank you!