如何从外部库中显示嵌入式资源图像,从后面的代码设置?
我有一个通用的库foo.common
,其中一些 *.pngs存储为嵌入式资源
或资源
。 现在,我想在我的foo.ui
winui3
应用程序中显示它们。
目前我无法再有任何进一步。我已经尝试了以下事情而没有成功...
<Image Source="ms-appx://Foo.Common/Assets/Images/foo.png"/>
<Image Source="ms-appx:///Foo.Common/Assets/Images/foo.png"/>
new Image
{
Source = new BitmapImage { UriSource = new Uri("ms-appx://Foo.Common/Assets/Images/foo.png") }
}
更新:
从后面的代码中获取嵌入式资源
,您可以使用此(
public static class StreamExtensions
{
public static async Task<BitmapImage> ToBitmapImageAsync(this Stream stream)
{
var bitmap = new BitmapImage();
using (var memStream = new MemoryStream())
{
await stream.CopyToAsync(memStream);
memStream.Position = 0;
bitmap.SetSource(memStream.AsRandomAccessStream());
}
return bitmap;
}
public static async Task<SvgImageSource> ToSvgImageSourceAsync(this Stream stream)
{
var bitmap = new SvgImageSource();
using (var memStream = new MemoryStream())
{
await stream.CopyToAsync(memStream);
memStream.Position = 0;
await bitmap.SetSourceAsync(memStream.AsRandomAccessStream());
}
return bitmap;
}
}
实际上,我仍然坚持如何在XAML
中使用它。
I have a common library foo.common
with some *.pngs stored as Embedded Resource
or Resource
.
Now I would like to show them in my foo.ui
WinUI3
application.
I can't get any further at the moment. I've tried the following things without success...
<Image Source="ms-appx://Foo.Common/Assets/Images/foo.png"/>
<Image Source="ms-appx:///Foo.Common/Assets/Images/foo.png"/>
new Image
{
Source = new BitmapImage { UriSource = new Uri("ms-appx://Foo.Common/Assets/Images/foo.png") }
}
UPDATE:
To get the Embedded Resource
from code behind, you can use this (https://stackoverflow.com/a/37010020/6229375) solution. To convert the stream
afterwards into an ImageSource
, you can use one of the following extension methods.
public static class StreamExtensions
{
public static async Task<BitmapImage> ToBitmapImageAsync(this Stream stream)
{
var bitmap = new BitmapImage();
using (var memStream = new MemoryStream())
{
await stream.CopyToAsync(memStream);
memStream.Position = 0;
bitmap.SetSource(memStream.AsRandomAccessStream());
}
return bitmap;
}
public static async Task<SvgImageSource> ToSvgImageSourceAsync(this Stream stream)
{
var bitmap = new SvgImageSource();
using (var memStream = new MemoryStream())
{
await stream.CopyToAsync(memStream);
memStream.Position = 0;
await bitmap.SetSourceAsync(memStream.AsRandomAccessStream());
}
return bitmap;
}
}
I'm actually still stuck how to get it working in xaml
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论