Results 1 to 2 of 2

Thread: [RESOLVED] Read PNG 16 bit greyscale file pixels

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Resolved [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?

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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;
    
            }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width