开发者

Android Java to C# URL

开发者 https://www.devze.com 2023-02-20 17:20 出处:网络
What is the C# equivalent of the following Java snippet below: Drawable image; URL imageUrl; imageUrl = new URL(getMyImageUrl(im开发者_开发百科ageNumber));

What is the C# equivalent of the following Java snippet below:

Drawable image;
URL imageUrl;

imageUrl = new URL(getMyImageUrl(im开发者_开发百科ageNumber));
Bitmap bitmap = BitmapFactory.decodeStream(imageUrl.openStream());
image = new BitmapDrawable(bitmap);

Thanks in advance.


A more literal conversion to C# would be:

var imageUrl = new Java.Net.URL(GetMyImageUrl(imageNumber));
var bitmap   = Android.Graphics.BitmapFactory.DecodeStream (imageUrl.OpenStream ());
var image    = new Android.Graphics.Drawables.BitmapDrawable (bitmap);

This is one of the strengths of Mono for Android: the classes and methods mirror the underlying Java platform (with some exceptions) while providing much of the .NET framework, so migrating code from Java to C# should be reasonably straightforward.


  using System.Drawing;
  using System.Drawing.Imaging;

  public Bitmap DownloadImage(string imageUrl)
  {
        try
        {
              WebClient client = new WebClient();

              using(Stream stream = client.OpenRead(imageUrl))
              {
                    Bitmap bitmap = new Bitmap(stream);
              }
        }
        catch(Exception)
        {
              //todo: handle me
              throw;
        }

        return bitmap
  }


Have a look at http://www.dreamincode.net/code/snippet2555.htm . I assumed you would want to use Bitmap. I have never used Drawable in Java, so correct me if I'm wrong.

0

精彩评论

暂无评论...
验证码 换一张
取 消