I'm creating a bitmap image from a local image file to overlay on another bitmap, but when specify the file URI I get a System.UriFormatException which I cant understand as the image is stored in a folder named Images in the project which seems correct. This is how I specified the URI new Uri("Images/boxbag.png"). Can someone explain why I'm getting this format exception,is it a problem with the file path or the way in which I set up the URI.

This is the complete method below which should clarify things more:

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"));
        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;
    }
}
有帮助吗?

解决方案

According to http://msdn.microsoft.com/en-us/library/z6c2z492%28v=vs.110%29.aspx:

This constructor assumes that the string parameter references an absolute URI and is equivalent to calling the Uri constructor with UriKind set to Absolute. If the string parameter passed to the constructor is a relative URI, this constructor will throw a UriFormatException.

First, try to use an absolute path to see if everything works in principle.

If it works, you can try to find the application path, e.g. with help of System.AppDomain.CurrentDomain.BaseDirectory, and construct an absolute path to the image using application path and the relative path from the application path to the image.

Something like the following:

string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string imageRelativePath = "...";
string imagePath = Path.Combine(baseDirectory, imageRelativePath);

其他提示

new Uri("Images/boxbag.png", UriKind.Relative)

Otherwise the uri is treated as absolute and must include scheme.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top