如何修复DirectoryNotFoundException?

时间:2014-02-23 13:15:03

标签: c# wpf directory

我正在尝试将本地图像叠加到Kinect传感器的视频输入上,但是当我覆盖图像时,即使图像目录看起来是正确的,我也会得到DirectoryNotFoundException。是否有人有解释至于为什么我会收到此错误或如何修复它?

在此行代码处抛出DirectoryNotFoundException:

drawingContext.DrawImage(overlayImage, new Rect(12, 12, overlayImage.Width, overlayImage.Height));

以下完整的方法是从Kinect传感器获取视频,并将图像覆盖到视频输入。当我使用代码绘制叠加层时程序崩溃但没有它就可以正常工作。

void myKinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
{
    using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
    {
        if (colorFrame == null) return;
        byte[] colorData = new byte[colorFrame.PixelDataLength];
        colorFrame.CopyPixelDataTo(colorData);
        KinectVideo.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96,
                PixelFormats.Bgr32, null, colorData, colorFrame.Width * colorFrame.BytesPerPixel);

        //drawing image overlay to video feed
        var drawingVisual = new DrawingVisual();
        var drawingContext = drawingVisual.RenderOpen();
        drawingContext.DrawImage(BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, colorData, colorFrame.Width * colorFrame.BytesPerPixel), 
                new Rect(new Size(colorFrame.Width, colorFrame.Height)));
        var overlayImage = new BitmapImage(new Uri("Images/boxbag.png", UriKind.Relative));
        //Error thrown here -->
        drawingContext.DrawImage(overlayImage, new Rect(12, 12, overlayImage.Width, overlayImage.Height));
        drawingContext.Close();
        var mergedImage = new RenderTargetBitmap(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Pbgra32);
        mergedImage.Render(drawingVisual);
        KinectVideo.Source = mergedImage;
    }
}

这是Visual Studio中显示的完整错误,我从错误中理解的是文件夹中没有图像,但没有意义,因为图像存储在名为Images的文件夹中在项目解决方案中。

An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in PresentationCore.dll

Additional information: Could not find a part of the path 'C:\Users\Brian Smith\documents\visual studio 2013\Projects\KinectKickboxingBVversion1\KinectKickboxingBVversion1\bin\x86\Debug\Images\boxbag.png'

根据VS,这是图像的完整路径:

C:\Users\Brian Smith\documents\visual studio 2013\Projects\KinectKickboxingBVversion1\KinectKickboxingBVversion1\Images\boxbag.png

1 个答案:

答案 0 :(得分:1)

请尝试此操作(可能需要调整程序集的名称):

var overlayImage
  = new BitmapImage(new Uri("pack://application:,,,/KinectKickboxingBVversion1;component/Images/boxbag.png"));

更多关于Pack URIs