3

I am taking an image, decoding the byte array that is generated by TakePicture method and then rotating the bitmap 270 degrees. The problem is that i seem to run out of memory, and i do not know how to solve it. Here is the code:

Bitmap image = BitmapFactory.DecodeByteArray (data, 0, data.Length);
data = null;
Bitmap rotatedBitmap = Bitmap.CreateBitmap (image, 0, 0, image.Width,
    image.Height, matrix, true);
18
  • Make your images smaller. Commented Feb 7, 2016 at 14:15
  • This might help stackoverflow.com/questions/2968397/… Commented Feb 7, 2016 at 14:24
  • i cannot make images smaller.. i need them as big as posible for OCR Commented Feb 7, 2016 at 14:32
  • @interjaz i don`t understand how that helps me Commented Feb 7, 2016 at 14:33
  • How big are your images? Commented Feb 7, 2016 at 14:35

1 Answer 1

2

Please have a look at Load Large Bitmaps Efficiently from official Xamarin documentation, which explains how you can load large images into memory without the application throwing an OutOfMemoryException by loading a smaller subsampled version in memory.

Read Bitmap Dimensions and Type

async Task<BitmapFactory.Options> GetBitmapOptionsOfImageAsync()
{
    BitmapFactory.Options options = new BitmapFactory.Options
    {
        InJustDecodeBounds = true
    };

    // The result will be null because InJustDecodeBounds == true.
    Bitmap result=  await BitmapFactory.DecodeResourceAsync(Resources, Resource.Drawable.someImage, options);

    int imageHeight = options.OutHeight;
    int imageWidth = options.OutWidth;

    _originalDimensions.Text = string.Format("Original Size= {0}x{1}", imageWidth, imageHeight);

    return options;
}

Load a Scaled Down Version into Memory

public static int CalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
    // Raw height and width of image
    float height = options.OutHeight;
    float width = options.OutWidth;
    double inSampleSize = 1D;

    if (height > reqHeight || width > reqWidth)
    {
        int halfHeight = (int)(height / 2);
        int halfWidth = (int)(width / 2);

        // Calculate a inSampleSize that is a power of 2 - the decoder will use a value that is a power of two anyway.
        while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth)
        {
            inSampleSize *= 2;
        }
    }

    return (int)inSampleSize;
}

Load the Image as Async

public async Task<Bitmap> LoadScaledDownBitmapForDisplayAsync(Resources res, BitmapFactory.Options options, int reqWidth, int reqHeight)
{
    // Calculate inSampleSize
    options.InSampleSize = CalculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.InJustDecodeBounds = false;

    return await BitmapFactory.DecodeResourceAsync(res, Resource.Drawable.someImage, options);
}

And call it to load Image, say in OnCreate

protected async override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);
    _imageView = FindViewById<ImageView>(Resource.Id.resized_imageview);

    BitmapFactory.Options options = await GetBitmapOptionsOfImageAsync();
    Bitmap bitmapToDisplay = await LoadScaledDownBitmapForDisplayAsync (Resources, 
                             options, 
                             150,  //for 150 X 150 resolution
                             150);
    _imageView.SetImageBitmap(bitmapToDisplay);
}

Hope it helps.

Sign up to request clarification or add additional context in comments.

4 Comments

i need the bitmap as big as it can be
This sample is given for loading 150 X 150, please tweak it load the resolution you wanted for you app. @BogdanConstantin
I can't seem to use Resource.Drawable.samoyed - what is that?
it's just an example, say, some image in your Drawable. corrected to avoid confusion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.