tags:

views:

127

answers:

1
[StructLayout(LayoutKind.Sequential)]
public struct PixelColor
{
 public byte Blue;
 public byte Green;
 public byte Red;
 public byte Alpha;
}

public PixelColor[,] GetPixels(BitmapSource source)
{
 if(source.PixelFormat!=PixelFormats.Bgra32)
 source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);

 int width = source.PixelWidth;
 int height = source.PixelHeight;
 PixelColor[,] result = new PixelColor[width, height];

 source.CopyPixels(result, width * 4, 0);
 return pixels;
}

I get this error message Input array is not a valid rank. Parameter name: pixels on this line source.CopyPixels(result, width * 4, 0);

Does anyone know what the problem is?

+1  A: 

BitmapSource.CopyPixels expects a one-dimensional array as the first parameter. You are passing it a two-dimensional array.

Instead of actually providing a "rectangular" array of bits, CopyPixels apparently gives you a single continuous array of them - the "stride" specifies the width of one scanline of the bitmap, which means - if I understand this correctly - that given a stride of n bits, the second row of the bitmap begins at bit n + 1

See this link for an explanation of stride: http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.drawing/2006-09/msg00057.html

djacobson