Recursive AI

Read and write a 16-bit grayscale PNG with alpha in C#

OneImage is a free, open-source image library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Keep grayscale and opacity as two 16-bit channels. Try the two-pixel gray-alpha.png from the download.

Terminal
dotnet add package RecursiveAI.OneImage

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneImage;
 
var img = Image.Load("gray-alpha.png");
img.Save("round-trip.png");

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

Writes round-trip.png as 16-bit grayscale with alpha (PNG color type 4).

The full program

The input is a 2 × 1 grayscale PNG with these 16-bit samples:

  • Left pixel: gray 12345, alpha 23456.
  • Right pixel: gray 60001, alpha 65535.
C#
using OneImage;
 
var image = Image.Load("gray-alpha.png");
image.Save("round-trip.png");
 
var saved = Image.Load("round-trip.png");
var header = File.ReadAllBytes("round-trip.png");
Console.WriteLine($"PNG depth: {header[24]}; type: {header[25]}");
var inputPixels = image.ToArray<GrayAlpha16>();
var outputPixels = saved.ToArray<GrayAlpha16>();
Console.WriteLine($"Samples match: {inputPixels.SequenceEqual(outputPixels)}");
foreach (var pixel in outputPixels)
    Console.WriteLine($"Gray {pixel.V}; alpha {pixel.A}");
Text
PNG depth: 16; type: 4
Samples match: True
Gray 12345; alpha 23456
Gray 60001; alpha 65535

The program reads the bit depth and color type from the output PNG’s IHDR header, then compares both channels after decoding. To check your own input before saving, compare image.Format to PixelFormat.GrayAlpha16: loading an ordinary RGB or 8-bit PNG does not make it 16-bit grayscale with alpha.

Variations

Create a grayscale PNG with alpha from two ushort pairs, instead of loading one:

C#
using OneImage;
 
var image = Image.FromPixels<GrayAlpha16>(
    [new(12345, 23456), new(60001, 65535)], 2, 1);
image.Save("from-samples.png");

The short answer

With OneImage 1.11.4, Image.Load keeps a 16-bit PNG with color type 4 in PixelFormat.GrayAlpha16. Save the loaded image as PNG without converting it to RGBA. Use ToArray<GrayAlpha16>() to read the gray value (V) and opacity (A) of each pixel as ushort values.

What is the bit depth of a grayscale image with alpha?

For a PNG, bit depth is per sample, not per pixel. According to the PNG specification’s IHDR table, color type 4 has one grayscale sample followed by one alpha sample; either both are 8-bit or both are 16-bit. At 16 bits, each sample can range from 0 to 65535, so the pair occupies 32 bits per pixel before compression. A 16-bit RGBA pixel uses 64 bits before compression. Those figures describe the samples, not the compressed file size.

GrayAlpha16 uses straight alpha: the gray sample is separate from opacity, not multiplied by it. In the example, the left pixel is partly transparent, the right one fully opaque. Saving and reloading keeps the two original values for each pixel. It is a pixel round trip, not a byte-for-byte copy of the PNG file.

Is 8-bit or 16-bit PNG better for grayscale?

Keep 16 bits if you need to preserve source values that differ by less than an 8-bit step, for example when another program will process the gray or alpha samples later. An 8-bit channel has only 256 possible values; once you reduce the precision, converting back to 16 bits cannot recover the distinctions you discarded. If you only need the displayed result and 256 levels per channel are enough, 8 bits can be a reasonable output choice.

A grayscale PNG with a tRNS transparency key is a different case. It names one gray value to treat as transparent instead of carrying an alpha sample for each pixel. OneImage 1.11.4 still decodes a 16-bit grayscale PNG with a transparency key as Rgba16; the two-channel round trip above is for PNG color type 4.

All oneimage recipes