简体   繁体   English

C#/ XAML-字符串到图像源的转换

[英]C#/XAML - String to Image Source Conversion

Hoping I can get some opinions on the issue I'm having. 希望我能对我遇到的问题发表一些意见。 First, I'll explain that I did try suggestions from other answers of questions that are similar to what I'm experiencing, but without any luck. 首先,我将说明我确实尝试了其他问题的建议,这些问题与我所遇到的问题类似,但是没有任何运气。

The objective is to take a string and convert it to a path for an image element in my XAML code. 目的是获取一个字符串并将其转换为XAML代码中图像元素的路径。 Similar to how you can adjust a textblock's "text" property in the code-behind file with something like this, 类似于您可以使用以下方法在代码隐藏文件中调整文本块的“ text”属性的方式,

courseFaculty.Text = BL_PageContent.FacultyMember;

in which FacultyMember is a property from the associated BL_PageContent.cs file, I'd like to do the same but provide a string for the image's path. 其中FacultyMember是关联的BL_PageContent.cs文件的属性,我想做同样的事情,但是提供图像路径的字符串。

Here's my XAML for my image, which renders a blank "placeholder," if you will: 这是我的图片的XAML,如果您愿意,它将呈现一个空白的“占位符”:

<Image x:Name="courseFacultyPic" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="246,127,10,0" Height="117" Width="104"></Image>

Testing this more "manually," I've entered the source path as a Source property and had success: 更加“手动”测试,我已经将源路径作为Source属性输入并获得了成功:

<Image x:Name="courseFacultyPic" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="246,127,10,0" Height="117" Width="104" Source="facultyProf1.jpg"></Image>

Using something that was suggested to me OUGHT to render the image I intend into this "placeholder:" 使用我建议使用的方法将我想要的图像渲染到此“占位符”中:

string facultyPicPath = "collegeProf1.jpeg";
Uri imageUri = new Uri(facultyPicPath);
BitmapImage facultyImage = new BitmapImage(imageUri);
Image courseFacultyPic = new Image();
courseFacultyPic.Source = facultyImage;

This code block above would initiate from a user clicking a UI control. 上面的代码块将从用户单击UI控件开始。 When I try it, though, I get a runtime exception regarding the line where I am instantiating the Uri class. 但是,当我尝试它时,我在实例化Uri类所在的行中遇到了运行时异常。 The runtime exception is as follows: 运行时异常如下:

An exception of type 'System.UriFormatException' occurred in System.Private.Uri.dll but was not handled in user code Additional information: Invalid URI: The format of the URI could not be determined. System.Private.Uri.dll中发生类型'System.UriFormatException'的异常,但未在用户代码中处理。其他信息:无效的URI:无法确定URI的格式。

Any thoughts/suggestions/opinions welcome. 欢迎任何想法/建议/意见。 This is my first post and my first experience handling images within my app, and am self-taught, so please take that into consideration. 这是我的第一篇文章,也是我第一次在应用程序中处理图像的经验,并且是自学成才的,因此请考虑在内。 Many thanks! 非常感谢!

This is because XAML has built in converters to take the image name like that and set it as an image source. 这是因为XAML内置了转换器,以采用类似的图像名称并将其设置为图像源。 When you use new BitmapImage(imageUri) you should specify the full image path. 使用新的BitmapImage(imageUri)时,应指定完整的图像路径。

If you are using WPF you can use the ImageSourceConverter but I don't believe this is available for UWP apps. 如果您使用的是WPF,则可以使用ImageSourceConverter,但我认为UWP应用程序不提供此功能。 I'm not sure... However; 我不确定...但是; it's not to hard to write your own once your own. 一次写自己的书并不难。

https://msdn.microsoft.com/en-us/library/system.windows.media.imagesourceconverter.convertfromstring(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/system.windows.media.imagesourceconverter.convertfromstring(v=vs.110).aspx

I ever got that error too. 我也曾经遇到过这个错误。 The solution is you must specify manually what kind of URI is that. 解决方案是您必须手动指定那是哪种URI。 From my experience, assuming that you have added the image to the project in the same directory as the application source code files, the URI kind you must specify is UriKind .Relative . 根据我的经验,假设您已将图像与应用程序源代码文件添加到项目的同一目录中,则必须指定的URI类型为UriKind .Relative

In the Uri constructor, specify it in the second parameter like this: Uri构造函数中,在第二个参数中指定它,如下所示:

Uri imageUri = new Uri(facultyPicPath, UriKind.Relative);

Here's the solution to my problem. 这是我的问题的解决方案。 Thanks to Rizki ! 感谢Rizki

The given System.Uri cannot be converted into a Windows.Foundation.Uri 给定的System.Uri无法转换为Windows.Foundation.Uri

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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