如何在Xamarin.Forms中使用位图?

3
我该如何在Xamarin Forms中使用位图?这个在我的Android项目中曾经用过,但我想从Xamarin Forms中使用类似的东西。我不想做任何渲染或平台特定的操作。如果我能在Forms中完成,那就太好了。你有什么想法吗?
     private async void GetIcon()
        {
            Weather weather = await Core.GetWeather();

            var bitmap = new Image { Source = ImageSource.FromUri(new Uri("http://openweathermap.org/img/w/" + weather.Icon + ".png")) };

DisplayIcon.bitmap ?

        }

XAML:

 <Image x:Name="DisplayIcon" Source ="{Binding bitmap}" />

ends here *******

****old post****

private Bitmap GetImageBitmapFromUrl(string url)
        {
            Bitmap imageBitmap = null;

            using (var webClient = new WebClient())
            {
                var imageBytes = webClient.DownloadData(url);
                if (imageBytes != null && imageBytes.Length > 0)
                {
                    imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
                }
            }

            return imageBitmap;
        }

        private async void displayIcon()
        {
            Weather weather = await Core.GetWeather();
            if (weather != null)
            {

                var bitmap = GetImageBitmapFromUrl("http://openweathermap.org/img/w/" + weather.Icon + ".png");
                Icon2.SetImageBitmap(bitmap);

            }
        }

3
https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/ - Jason
2个回答

0

SkiaSharp对位图有广泛的支持,提供大量示例,无需渲染器或平台代码。(针对位图)


如果你看他的例子,他只是加载png文件,而不是bmp文件。 - Jason
对,我没注意到!在这种情况下,只需要一个ImageView,是吗? - Hichame Yessou
请检查我的评论。我可能错过了如何使用XAML显示实际图像。谢谢! - Pxaml
@Pxaml 那是一个不同的问题,对于这个问题,你应该遵循Jason的评论或者查看这些示例 https://github.com/xamarin/xamarin-forms-samples/tree/master/WorkingWithImages - Hichame Yessou
好的,如果您查看我的更新,那就是我所做的。 - Pxaml

0

由于某些原因,绑定图像的效果并不如我预期。然而,我找到了这个解决方法作为一种解决方案。

我将其添加到C#中。

var bitmap = new Image { Source = ImageSource.FromUri(new Uri("http://openweathermap.org/img/w/" + weather.Icon + ".png")) };

            DisplayIcon.Source = bitmap.Source;

并将其转换为 XAML

<Image x:Name="DisplayIcon"/>

感谢大家的答复。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接