2

I am a newbie in Xamarin. I have been trying to set an image to Imagebutton from specific a URL in C# for Xamarin. 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... :)

1 Answer 1

3

To set the ‘default’ image for a button from local images, call SetImage

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

To apply from an 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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

'GetImageBitmapFromUrl' is that a seperate method or a native method. coz error throws up like GetImageBitmapFromUrl does not exist in current context
Thanks for the quick reply... I facing an error lyk namespace for bitmap could not be found.. Vl fix the code and let u knw..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.