1

我想使用此代码访问要与我的 Kinect 代码一起使用的图像像素。所以我可以用图像位替换深度所以我创建了一个 WPF 应用程序,一旦我运行我的代码,我就会得到这个异常(它不会发生在控制台应用程序上)但我需要它作为WPF 应用程序运行,因为 . . 我打算将它与Kinect一起使用

XamlParseException

'对匹配指定绑定约束的'pixelManipulation.MainWindow'类型的构造函数的调用引发了异常。行号“3”和行位置“9”。

这是代码:

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

1 回答 1

1

问题出在您的 xaml 文件中,而不是在您的代码中。由于异常状态xaml 解析异常。我的猜测是您在 xaml 中声明了一些事件处理程序/属性来绑定到不再存在的东西。发布 xaml 文件内容以获得更多帮助。

编辑

所以这不是它看起来的样子。Xaml 文件可以,但代码不行。线上构造函数抛出异常

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

我不确定为什么对位图构造函数的调用无效,但将其更改为:

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

应该可以正常工作。

于 2012-07-19T10:50:30.993 回答