12

I want to load an image from url to imageview in c# ( android programming ) after search in google i cant find any good result , thank you for helping

i am using xamarin studio

1
  • Try the Picasso component Commented Aug 16, 2017 at 15:22

4 Answers 4

26

The very first hit I got from Google was a thread on the Xamarin forums discussing this exact issue:

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

var imageBitmap = GetImageBitmapFromUrl("http://xamarin.com/resources/design/home/devices.png");
imagen.SetImageBitmap(imageBitmap);
Sign up to request clarification or add additional context in comments.

Comments

1

Both approaches work, but is a good practice to do it asynchronously. Here you have some good examples:

  1. Asynchronous Image Loading in Xamarin Android http://javatechig.com/xamarin/asynchronous-image-loading-xamarin-android
  2. xamarin-store-app image helper https://github.com/xamarin/xamarin-store-app/blob/master/XamarinStore.Droid/Helpers/Images.cs

Comments

1

Am using the below class in Xamarin Android:

public class DownloadImageTask : AsyncTask
{
    private ImageView bmImage;
    private ProgressBar progressBar;

    public DownloadImageTask( ImageView bmImage , ProgressBar progressBar)
    {
        this.bmImage = bmImage;
        this.progressBar = progressBar;
    }

    protected override void OnPostExecute( Object result )
    {
        base.OnPostExecute(result);
        bmImage.SetImageBitmap(( Bitmap ) result);
        if (progressBar != null)
            progressBar.Visibility = ViewStates.Gone;
    }


    protected override Object DoInBackground( params Object[] @params )
    {
        var urldisplay = @params[0].ToString();
        Bitmap mIcon11 = null;
        try
        {
            var req = WebRequest.Create(urldisplay);
            var response = req.GetResponse();
            var stream = response.GetResponseStream();

            mIcon11 = BitmapFactory.DecodeStream(stream);
        }
        catch ( Exception e )
        {

        }
        return mIcon11;
    }
}

Execution :

new DownloadImageTask(imgProfile , progressBar).Execute(uri);

Comments

-1

I did this to load an Svg from an url into an ImageView using SkiaSharp.

In the .xml

<ImageView
    android:contentDescription=""
    android:id="@+id/video_recorder_image"
    android:layout_width="wrap_content"
    android:layout_height="50dp" />

In the activity/fragment.

private ImageView iconImageView;

public override void OnViewCreated(View view, Bundle savedInstanceState)
{
    iconImageView = (ImageView)view.FindViewById(Resource.Id.video_recorder_image);
    Bitmap image = GetImageBitmapFromUrl(_iconUrl);
}

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

    using (var webClient = new WebClient())
    {
        var imageBytes = webClient.DownloadData(url);
        if (imageBytes != null && imageBytes.Length > 0)
        {
            var svgContent = Convert.ToBase64String(imageBytes, 0, imageBytes.Length);
            var byteArray = Convert.FromBase64String(svgContent);

            using (var stream = new MemoryStream(byteArray))
            {
                var bitmap = new SKBitmap(500, 500);
                var canvas = new SKCanvas(bitmap);

                // load the SVG
                var svg = new SkiaSharp.Extended.Svg.SKSvg(new SKSize(500, 500));
                svg.Load(stream);

                // draw the SVG to the bitmap
                canvas.DrawPicture(svg.Picture);
                var skData = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100);

                // Convert image to string and then to Bitmap
                var convertedSvgStream = skData.AsStream();
                var convertedImageBytes = new byte[(int)convertedSvgStream.Length];
                convertedSvgStream.Seek(0, SeekOrigin.Begin);
                convertedSvgStream.Read(convertedImageBytes, 0, (int)convertedSvgStream.Length);

                imageBitmap = BitmapFactory.DecodeByteArray(convertedImageBytes, 0, convertedImageBytes.Length);
            }
        }
    }

    return imageBitmap;
}

Comments

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.