[RESOLVED] Read PNG 16 bit greyscale file pixels
I have a PNG file which is a greyscale image and I want to do calculations on the data within it. I don't need to display it, I just need to get at the pixel data and assemble a 2D array of the 16 bit values.
But... I don't want to convert it to a Bitmap (been down that route already) because I lose the 16 bit resolution.
Is there a way I can read the PNG pixel data?, or maybe a way I can convert it directly to a 48bit Bitmap and go from there?
Re: Read PNG 16 bit greyscale file pixels
I found a solution which I shall put here for posterity...
Download FreeImage from freeimage.sourceforge.net/download.html, compile the DLL for .Net and after adding a reference to it, you can retrieve the pixel data as below.
Code:
using FreeImageAPI;
private List<List<int>> LoadPixelsFromFile(string filename)
{
FIBITMAP dib = FreeImage.LoadEx(filename);
int Height = (int)FreeImage.GetHeight(dib);
int Width = (int)FreeImage.GetWidth(dib);
List<List<int>> ImageData = new List<List<int>> { };
for (int y = 0; y < Height; y++)
{
List<int> row = new List<int> { };
// Note: dib is stored upside down
Scanline<ushort> line = new Scanline<ushort>(dib, Height - 1 - y);
foreach (ushort pixel in line)
{
row.Add(pixel);
}
ImageData.Add(row);
}
FreeImage.UnloadEx(ref dib);
return ImageData;
}