【问题标题】:how to use Bitmap for Xamarin.Forms?如何为 Xamarin.Forms 使用位图?
【发布时间】:2018-07-24 04:22:17
【问题描述】:

如何使用 Xamarin Forms 中的位图?这曾经在我的 Android 项目中,但我想使用 Xamarin 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}" />

到此结束 *******

****旧帖****

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);

            }
        }

【问题讨论】:

标签: bitmap xamarin.forms icons


【解决方案1】:

SkiaSharp 对位图有广泛的支持,有大量的examples,不需要渲染器或平台代码。 (用于位图)

【讨论】:

  • 如果你看他的例子,他只是在加载 png,而不是 bmp
  • 对,我没注意!在这种情况下,它只需要一个 Image 视图,不是吗?
  • 请查看我的评论。我可能会错过如何使用 xaml 显示实际图像。谢谢!
  • @Pxaml 这是一个不同的问题,因为您应该关注 Jason 的评论或查看示例github.com/xamarin/xamarin-forms-samples/tree/master/…
  • 好吧,如果你检查我的更新,这正是我所做的
【解决方案2】:

由于某种原因,绑定图像没有像我预期的那样工作。但是,我发现此解决方法是一种解决方案。

我将它添加到 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"/>

谢谢大家的回答。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2017-08-05
    • 2017-05-20
    • 1970-01-01
    相关资源
    最近更新 更多