[英]How to add a URI path to a bitmap image?
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. 我正在从本地图像文件创建一个位图图像以覆盖另一个位图,但是当指定文件URI时,我得到一个
System.UriFormatException
,我无法理解,因为图像存储在项目中名为Images的文件夹中,这似乎是正确的。 This is how I specified the URI new Uri("Images/boxbag.png")
. 这就是我指定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. 有人可以解释为什么我得到这种格式异常,是文件路径的问题还是我设置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 : 根据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.
此构造函数假定string参数引用绝对URI,并且等效于在UriKind设置为Absolute时调用Uri构造函数。 If the string parameter passed to the constructor is a relative URI, this constructor will throw a UriFormatException.
如果传递给构造函数的字符串参数是相对URI,则此构造函数将抛出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, eg 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. 如果可行,您可以尝试查找应用程序路径,例如在
System.AppDomain.CurrentDomain.BaseDirectory
帮助下,使用应用程序路径和从应用程序路径到映像的相对路径构建映像的绝对路径。
Something like the following: 类似于以下内容:
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string imageRelativePath = "...";
string imagePath = Path.Combine(baseDirectory, imageRelativePath);
new Uri("Images/boxbag.png", UriKind.Relative)
否则,uri被视为绝对的,必须包括方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.