简体   繁体   中英

XamlParseException - Image Pixels Manipulation with WPF C# / Kinect

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

The issue is in your xaml file not in your code. As exception states xaml parse exception . My guess is that you had some event handler / property declared in xaml to bind to something that no longer exists. Post xaml file content for more help.

Edit

So it is not what it seams to be. Xaml file is OK, But code is not. There is an exception thrown in constructor on line

System.Drawing.Bitmap b = new
            System.Drawing.Bitmap(@"autumn_scene.jpg");

I'm not sure why this call to bitmap constructor is invalid but changing it to:

System.Drawing.Bitmap b = new Bitmap(
            System.Drawing.Image.FromFile(@"autumn_scene.jpg"));

should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM