Search code examples
xamarinxamarin.formsbitmapxamarin.android

Get image from Drawable folder in bitmap


How can get image from drawable folder for BitmapFactory.DecodeResource. i tried this.                       

img name image kept in drawable folder then this

 Bitmap originalImage = BitmapFactory.DecodeResource(Resource.Drawable.img,0);

but it's generate a error

"Can not convert from int to Android.Content.Res.Resources"


Solution

  • i am able to get drawable image for bitmap decode resource. I used this code.

    string imagefileName = "imgimge.png";
                            // Remove the file extention from the image filename
                            imagefileName = imagefileName.Replace(".jpg", "").Replace(".png", "");
    
                            // Retrieving the local Resource ID from the name
                            int id = (int)typeof(Resource.Drawable).GetField(imagefileName).GetValue(null);
    
                            // Converting Drawable Resource to Bitmap
                            var originalImage = BitmapFactory.DecodeResource(Xamarin.Forms.Forms.Context.Resources, id);