如何从外部库中显示嵌入式资源图像,从后面的代码设置?

发布于 2025-02-13 00:29:36 字数 1577 浏览 0 评论 0原文

我有一个通用的库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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文