简体   繁体   中英

Load image from url to ImageView - C#

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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