I want to use this code to access the image pixels to be used with my Kinect code.. so i can replace the depth bits with the Image bits, so i created a WPF application and As soon as i run my code, i get this exception (it doesnt happen on a Console application) but i need this to run as a WPF application since . . i intend to use it with Kinect
XamlParseException
'The invocation of the constructor on type 'pixelManipulation.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.
Here is the code:
public partial class MainWindow : Window
{
System.Drawing.Bitmap b = new
System.Drawing.Bitmap(@"autumn_scene.jpg");
public MainWindow()
{
InitializeComponent();
doSomethingWithBitmapFast(b);
}
public static void doSomethingWithBitmapFast(System.Drawing.Bitmap bmp)
{
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadOnly,
bmp.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr,
rgbValues, 0, bytes);
byte red = 0;
byte green = 0;
byte blue = 0;
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
//See the link above for an explanation
//of this calculation (assumes 24bppRgb format)
int position = (y * bmpData.Stride) + (x * 3);
blue = rgbValues[position];
green = rgbValues[position + 1];
red = rgbValues[position + 2];
Console.WriteLine("Fast: " + red + " "
+ green + " " + blue);
}
}
bmp.UnlockBits(bmpData);
}
}
}