View Large bitmap image

GamalAhmedGamalAhmed ✭✭EGMember ✭✭

I'd like to View large image in my View Background and it failed with imageView
What i can do ?

Posts

  • xamarinxxamarinx ✭✭ SAMember ✭✭

    You can resize the image before display.

  • JonDouglasJonDouglas Xamurai USXamarin Team, University, Developer Group Leader Xamurai

    There's a whole section of documentation on this topic that you can read over here:

    https://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/

  • GamalAhmedGamalAhmed ✭✭ EGMember ✭✭

    Mohammad Alsoyhri how to resize it ?

  • BerayBentesenBerayBentesen ✭✭✭✭ TRUniversity ✭✭✭✭

    @GamalAhmed use Glide. It handles large images and also caches for you.

  • GamalAhmedGamalAhmed ✭✭ EGMember ✭✭

    @BerayBentesen my app is Send and receive byte array that's converted to Bitmap Image , Do Glide Accepted Byte array or Bitmap Image ? i see that is take URL

  • xamarinxxamarinx ✭✭ SAMember ✭✭
    put this under MainActivity
    public static readonly int PickImageId = 1000;
    
    like this : 
    public class MainActivity : Activity
        {       
           public static readonly int PickImageId = 1000;
    
    then put these anywhere in Activity : 
    
    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
            {
                if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
                {            
                    var imageView1 = FindViewById<ImageView>(Resource.Id.imageView1);
                    Stream stream = ContentResolver.OpenInputStream(data.Data);
                    imageView1.SetImageBitmap(DecodeBitmapFromStream(data.Data, 150, 150));
                    Bitmap bitmap = BitmapFactory.DecodeStream(stream);
                    MemoryStream memStream = new MemoryStream();
                                               //you can change 60 (100 big size reduce to less >>>>
                    bitmap.Compress(Bitmap.CompressFormat.Jpeg, 60, memStream);
                    byte[] picData = memStream.ToArray();
    
            //picData is byte array you can use it.
                }
            }
    
            private Bitmap DecodeBitmapFromStream(Android.Net.Uri data, int requestedWidth, int requestedHeight)
            {            
                Stream stream = ContentResolver.OpenInputStream(data);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.InJustDecodeBounds = true;
                BitmapFactory.DecodeStream(stream);
                options.InSampleSize = CalculateInSampleSize(options, requestedWidth, requestedHeight);
                stream = ContentResolver.OpenInputStream(data); //Must read again
                options.InJustDecodeBounds = false;
                Bitmap bitmap = BitmapFactory.DecodeStream(stream, null, options);
                return bitmap;
            }
    
    
            private int CalculateInSampleSize(BitmapFactory.Options options, int requestedWidth, int requestedHeight)
            {
                int height = options.OutHeight;
                int width = options.OutWidth;
                int inSampleSize = 1;
                if (height > requestedHeight || width > requestedWidth)
                {               
                    int halfHeight = height / 2;
                    int halfWidth = width / 2;
                    while ((halfHeight / inSampleSize) > requestedHeight && (halfWidth / inSampleSize) > requestedWidth)
                    {
                        inSampleSize *= 2;
                    }
                }
                return inSampleSize;
            }
    
            then the code of the Button like this : 
     btnimage.Click += delegate {           
                Intent = new Intent();
                Intent.SetType("image/*");
                Intent.SetAction(Intent.ActionGetContent);
                StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), PickImageId);
            };
    
    That's All
    
Sign In or Register to comment.