WPF Image:显示文件中的图像,如何?
我想在 WPF 中显示图像,但使用实际图像,而不是使用 URL。这样做的原因是因为我想序列化图像,这样我就可以轻松携带它们。如果 Image 类仅是 Source,我该怎么做?这意味着,使用 URI 。这个想法非常简单:序列化图像,然后,当反序列化完成时,它将图像加载到像 Image 这样的控制器上。 甚至有可能吗,假设我已经序列化了图像,那么我怎么可能获得它们的 URI,即使这样的事情甚至存在?这很令人困惑。希望得到一些真正的启发...
我已经找到了许多显示如何序列化/反序列化的代码片段,但老实说,我还无法测试它们,因为我计划的初始部分已经被搁置。不过,如果有人有一个清晰的链接,我将不胜感激!
I want to display images in WPF, but using an actual image, instead of using the URL. The reason to this is because i want to Serialize images, so i can carry them around easily. How can i do this, if Image class is Source only? Which means, uses URI . The idea would be very simple: Serialize image, then, when deserialization done, it would load the image onto a controller like Image.
Is it even possible to, lets say i had the images serialized, then how could i even get their URI, even such a thing even exists? This is very confusing. Hope for some true enlightenment...
I already found many code snippets that show how to serialize/deserialize but honestly i haven't been able to test them out yet, as the initial part of my plan is already on hold. Although, if anyone has a good clean link to it, i'd appreciate it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WPF 不需要实际的 URI;它需要一个
ImageSource
实例,并且恰好包含一个从string
到ImageSource
的内置转换器,该转换器将字符串解释为 URI。将序列化格式转换为
ImageSource
实例的最简单方法是将如下内容放入IValueConverter
中并返回BitmapImage
:WPF doesn't require an actual URI; it requires an instance of
ImageSource
and it happens to include a built-in converter fromstring
toImageSource
that interprets the string as a URI.The easiest way to convert your serialized format into an
ImageSource
instance is to put something like the below in anIValueConverter
and return theBitmapImage
:Image.Source
< /a> 不是 URI,您可以输入 URI,但它们只是自动转换为ImageSource
使用 <代码>类型转换器。因此,如果您想要序列化和反序列化图像,您可以将相应的ImageSource
公开为属性,然后可以将其设置为或绑定到Image.Source
。Image.Source
is not a URI, you can enter URIs but they are just automatically converted to anImageSource
using aTypeConverter
. So if you want to serialize and deserialize images you can expose a respectiveImageSource
as a property, this can then be set as or bound to theImage.Source
.