Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Resizing mutable bitmap in onDraw() causes blur bitmap

I want to draw bitmap. but my bitmap size varies dynamically so on each call of OnDraw() I was calling
newBitmap = Bitmap.CreateScaledBitmap(bitmap, Width, Height, true);

But as there are multiple bitmaps I want to draw on my canvas, the above line causes OutOfMemory error. So I declared this globally, converting bitmap to Mutable bitmap and just changing width and height of bitmap where I was writing above line.

If I place resulting mutable bitmap as it is, it is displayed proper but if I change its size, then image is not proper. Its blur and very poor quality.

Here is my code:

Getting Mutable bitmap:
BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true, InMutable = true, InDither = true, InPreferredConfig = Bitmap.Config.Argb8888 }; int resourceId = (int)typeof(Resource.Drawable).GetField(source).GetValue(null); Bitmap bitmap= BitmapFactory.DecodeResource(Resources, resourceId, options);

Ondraw -
protected override void OnDraw(Canvas canvas) { //newBitmap = Bitmap.CreateScaledBitmap(bitmap,500,300, true); // this causes memory issue canvas.DrawBitmap(bitmap, new Matrix(), new Paint()); // This gives me correct bitmap but its too large bitmap.Width = 500; bitmap.Height = 300; canvas.DrawBitmap(bitmap, new Matrix(), new Paint()); // Blur image here with specified width and height. }

Thank you !

Sign In or Register to comment.