简体   繁体   English

如何在图像按钮中显示来自 URL 的图像 - xamarin Android

[英]How to display an image from URL in imagebutton - xamarin Android

I am a newbie in Xamarin.我是 Xamarin 的新手。 I have been trying to set an image to Imagebutton from specific a URL in C# for Xamarin.我一直在尝试从 Xamarin 的 C# 中的特定 URL 将图像设置为 Imagebutton。 I googled and was unable to find a sample code or documentation.我用谷歌搜索并找不到示例代码或文档。 It would be helpful if any one provide me with a helpful documentation or a little bit of sample code to do this.如果有人向我提供有用的文档或一些示例代码来执行此操作,将会很有帮助。

Thanks In Advance... :)提前致谢... :)

To set the 'default' image for a button from local images, call SetImage要从本地图像为按钮设置“默认”图像,请调用 SetImage

button1 = UIButton.FromType(UIButtonType.RoundedRect);
button1.SetImage(UIImage.FromFile ("sample.png"), UIControlState.Normal);

To apply from an URL,要从 URL 申请,

button1  = FindViewById(Resource.Id.RoundedRect);
var imageBitmap = GetImageBitmapFromUrl("http://xamarin.com/resources/design/home/test.png");
button1.SetImageBitmap(imageBitmap);


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM