Hello everyone,
I would like to create a grayscale png image from a byte buffer containing the 8bits gray values. The following code show how i do until now. This is clearly not the best way so what is the good one ?
I don't know how to read my one channel pixels byte array .
Thank you for your help.
I would like to create a grayscale png image from a byte buffer containing the 8bits gray values. The following code show how i do until now. This is clearly not the best way so what is the good one ?
I don't know how to read my one channel pixels byte array .
Thank you for your help.
//convert raw buffer to grayscale 8 bits png image
static void SaveGrayscale8bitsPngImage(byte[] pixels, string path)
{
MagickReadSettings settings = new MagickReadSettings();
settings.Width = 640;
settings.Height = 480;
settings.PixelStorage = new PixelStorageSettings(StorageType.Char, "R");
using (MagickImage image = new MagickImage(pixels, settings))
{
image.Grayscale(PixelIntensityMethod.Average);
image.Format = MagickFormat.Png;
image.SetDefine(MagickFormat.Png, "color-type", "0"); //0 = grayscale image
image.SetDefine(MagickFormat.Png, "bit-depth", "8"); //number of bits per index
image.SetDefine(MagickFormat.Png, "compression-level", "9"); //valid values are 0 through 9, with 0 providing the least but fastest compression and 9 usually providing the best and always the slowest.
image.Write(path);
}
}