WPF Image:显示文件中的图像,如何?

发布于 2024-12-01 10:38:27 字数 317 浏览 0 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网名女生简单气质 2024-12-08 10:38:27

WPF 不需要实际的 URI;它需要一个 ImageSource 实例,并且恰好包含一个从 stringImageSource 的内置转换器,该转换器将字符串解释为 URI。

将序列化格式转换为 ImageSource 实例的最简单方法是将如下内容放入 IValueConverter 中并返回 BitmapImage

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = /*your serialized image data in Stream form*/;
bi.EndInit();

WPF doesn't require an actual URI; it requires an instance of ImageSource and it happens to include a built-in converter from string to ImageSource 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 an IValueConverter and return the BitmapImage:

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = /*your serialized image data in Stream form*/;
bi.EndInit();
南渊 2024-12-08 10:38:27

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 an ImageSource using a TypeConverter. So if you want to serialize and deserialize images you can expose a respective ImageSource as a property, this can then be set as or bound to the Image.Source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文