Each color becomes a grayscale PNG.
dotnet add package RecursiveAI.OneImageRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneImage; var rgb = Image.Load("rgb.png") .SplitChannels(); rgb[0].Save("red.png"); rgb[1].Save("green.png"); rgb[2].Save("blue.png");
Unzip the example, then dotnet run.
Writes red.png, green.png, and blue.png. The zip’s rgb.png is a three-pixel strip: (255, 0, 0), (0, 255, 0), (0, 0, 255) from left to right.
The full program
The zip reads back the three grayscale files and prints their sample values in pixel order. The white pixel (255) is in a different position in each file, so you can see which color channel you saved.
using OneImage; Image[] rgb = Image.Load("rgb.png").SplitChannels(); string[] names = ["red", "green", "blue"]; for (int i = 0; i < names.Length; i++) { string file = $"{names[i]}.png"; rgb[i].Save(file); // Read back the gray samples in the saved PNG, left to right. Gray8[] row = Image.Load(file).ToArray<Gray8>(); Console.WriteLine($"{file}: {string.Join(", ", row.Select(p => p.V))}"); }
red.png: 255, 0, 0 green.png: 0, 255, 0 blue.png: 0, 0, 255
Use an RGB or RGBA source for this snippet: a grayscale image has no red, green, or blue planes to index, and splitting CMYK or Lab throws ImageOperationException.
Variations
Export alpha as its own image. This one-pixel input has RGB values (10, 20, 30) and opacity 128 out of 255. The fourth output is that opacity as gray, not an alpha mask applied to another image.
using OneImage; var image = Image.FromPixels<Rgba32>( [new(10, 20, 30, 128)], 1, 1); var channels = image.SplitChannels(); channels[3].Save("alpha.png"); var alpha = Image.Load("alpha.png"); Console.WriteLine(alpha.ToArray<Gray8>()[0].V);
128Related recipes
The short answer
Call Image.Load(path).SplitChannels() on an RGB or RGBA image, then save results 0, 1, and 2 as grayscale PNGs. The order is red, green, blue. An RGBA image also returns alpha at index 3. This separates the channels stored in the image; it doesn’t tint the output red, green, or blue.
What is RGB channel splitting?
An RGB pixel stores three samples: red, green, and blue. Splitting puts each sample in its own grayscale image, keeping the same pixel coordinates. For example, a pixel with RGB values (200, 80, 20) becomes light gray in the red image, darker gray in the green image, and nearly black in the blue image. Pure red becomes white in the red image and black in the other two. White in the source is bright in all three; channel splitting isn’t a way to select only red objects.
This also isn’t the same as converting an image to grayscale. Grayscale conversion combines colors into one brightness value. Splitting keeps the three values separate, so you can inspect them one at a time without throwing the other two away. OneImage creates the channel images lazily and keeps the source sample type in memory. Saving a plane computes its pixels and writes them in a format supported by the encoder.
How do I extract RGB values from a transparent image?
Split an RGBA image the same way as an RGB image and read the first three results. The samples are straight color values: if a source stores colors already multiplied by opacity, SplitChannels un-multiplies those samples first. The color PNGs don’t carry the original transparency. A semitransparent red pixel can still look bright in red.png because that file measures red, not how much of the pixel you see when it is placed over a background.
Save the fourth plane if you need to inspect transparency or preserve it separately. In that plane, black is fully transparent and white is fully opaque. For an input of unknown type, don’t assume that any second or fourth file is another color: the method splits stored channels, so a grayscale image gives just gray, or gray then alpha. Check the source format before labeling output files as RGB.