A linear gradient blends colors along a line; a radial gradient blends them outward from a center.
dotnet add package RecursiveAI.OneRender.ImageRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneRender.Image; var image = HtmlRender.ToImage(""" <style> body { margin: 0 } div { height: 40px } .line { background: linear-gradient( to right, red 20%, blue 80%) } .ring { background: radial-gradient( circle 20px, red 20%, blue 80%) } </style> <div class="line"></div> <div class="ring"></div> """, new() { Width = 80, Height = 80 }); File.WriteAllBytes("gradients.png", image.ToPng()); Console.WriteLine("Saved gradients.png");
Writes an 80 × 80 PNG with a linear band above a radial band.
Saved gradients.pngUnzip the example, then dotnet run.
The full program
The HTML and CSS are inline; there are no files to fetch. The full program also samples three pixels in each band and prints their red, green, blue and alpha channels in hexadecimal (#RRGGBBAA).
using OneRender.Image; var image = HtmlRender.ToImage(""" <style> body { margin: 0 } div { width: 80px; height: 40px } .line { background: linear-gradient( to right, red 20%, blue 80%) } .ring { background: radial-gradient( circle 20px, red 20%, blue 80%) } </style> <div class="line"></div> <div class="ring"></div> """, new() { Width = 80, Height = 80 }); File.WriteAllBytes("gradients.png", image.ToPng()); Console.WriteLine("Saved gradients.png"); foreach (var (x, y) in new[] { (8, 20), (40, 20), (72, 20), (40, 60), (50, 60), (60, 60) }) { int offset = (y * image.Width + x) * 4; var rgba = image.Pixels.AsSpan(offset, 4); Console.WriteLine($"({x}, {y}) #{Convert.ToHexString(rgba)}"); }
Saved gradients.png (8, 20) #FF0000FF (40, 20) #7D0082FF (72, 20) #0000FFFF (40, 60) #FF0000FF (50, 60) #75008AFF (60, 60) #0000FFFF
If a band is missing when you adapt the example, check its height: the empty div elements need an explicit height to give the background an area to paint.
Variations
Replace the linear background with two stops at the same position to make a hard split:
background: linear-gradient( to right, red 50%, blue 50%);
The sample at (8, 20) stays red and the one at (40, 20) becomes #0000FFFF.
Related recipes
The short answer
Set linear-gradient() or radial-gradient() as the background of an HTML element with a visible area. Pass the HTML and the desired canvas size to HtmlRender.ToImage, then save ToPng(). You don’t need to draw the gradient in C# or read the PNG back to inspect it: the returned image already exposes its pixels.
What is the difference between a linear gradient and a radial gradient in CSS?
A linear gradient measures position along a line. The top band uses to right, so the color stays the same from top to bottom at a given x. Its stops at 20% and 80% of the 80-pixel line are 16 and 64 CSS pixels from the left. That’s why the left and right samples are solid colors, while (40, 20) falls in the blend.
A radial gradient measures distance outward from its center. The lower band uses a 20-pixel circle centered in the box, so the two stops are 4 and 16 CSS pixels from its center. The percentages are not widths measured across the box. At the center sample the color is red; 10 pixels to the right it is a mix; 20 pixels to the right it has already reached blue. The two bands use the same percentages but don’t put the transition at the same image coordinates.
How do I set color stops in a CSS gradient?
Write a color followed by a position, such as red 20%. In this non-repeating example the first color holds before the first stop and the last color holds after the last stop. Move the stops farther apart to give the blend more room, or put them at the same position for a sharp boundary as in the variation. Change the linear direction or the radial size separately from the stops: one sets the gradient’s geometry, the other controls where its colors change along that geometry.
When checking a rendered result, put samples clearly before, between and after the stops. The full program uses pixel indices from the upper left, with four consecutive bytes per pixel. An integer index names a pixel, not the mathematical boundary between pixels, so don’t expect the center index of an even-width image to land exactly on the 50% line. The display list reference describes how OneRender resolves stop positions and interpolates between them.