0

I'm readying pixel data from a PNG BitmapSource using CopyPixels method. However the read call into array does not return the pixel data.

Any reason why this would happen ?

I have included the image below

PNG Sample

Edit:

Byte array doesn't contain the RGB pixel data. Array just contains value 0 for each index item.

Code:

using(FileStream stream = new FileStream(strImageFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        if (stream.Length > 0)
        {
            BitmapDecoder bmpDecoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

            BitmapSource bmpSourceFrame = bmpDecoder.Frames[0];

            int iStride = bmpSourceFrame.PixelWidth * ((bmpSourceFrame.Format.BitsPerPixel + 7) / 8);
            byte[] bytImage = new byte[iStride * bmpSourceFrame.PixelHeight];

            bmpSourceFrame.CopyPixels(bytImage, iStride, 0);
        }
     }
Kartik150
  • 555
  • 1
  • 5
  • 19
  • If it does not return the pixel data, what does it return? What is the content of the array looking like after calling CopyPixels? Please also show the _relevant_ part of your code that attempts to copy the pixels. There is no fun in speculating about what your code might or might not do... (**edit** and improve your question to add this information) –  Feb 19 '19 at 22:09
  • @elgonzo i have now editted the question and included the code – Kartik150 Feb 19 '19 at 22:44
  • 1
    Your code looks fine. How many bytes in the byte array have you actually checked? Have you only checked a few bytes at the beginning of the array? As far as i can tell, the first two lines in your image are completely transparent, whose RGBA pixel values could very well be 0. Given you have 32bpp and an image width of 32 pixels, i wouldn't be surprised if the first ~240 bytes are actually zero before you come across a non-zero byte... –  Feb 20 '19 at 00:04
  • Best way to check if the array is indeed empty is by using a `for` loop to go over the entire thing, and making it store the index and end on encountering a non-0 value. – Nyerguds Mar 01 '19 at 09:02

0 Answers0