locked
[UWP]'Insufficient memory to continue the execution of the program' while calling BitmapEncoder.SetPixelData RRS feed

  • Question

  • Hi,

    I want to capture a specific view and convert to an image using RenderTargetBitmap and save the image in local folder. But I am getting 'Insufficient memory to continue the execution of the program' while calling BitmapEncoder.SetPixelData() method. Please find the below code snippet.

    BitmapEncoder renderedEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, mrenderedStream);
    renderedEncoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied,
                    (uint)imageWidth, (uint)imageHeight, 120, 120, mergedPixels);
               
    await renderedEncoder.FlushAsync();

    mergedPixels.Length is 80621568. 

    (Exception from HRESULT: 0x80030008 (STG_E_INSUFFICIENTMEMORY))'

    I have loaded image in layout and its size is (5184 x 3888) and memory size 5 MB. I do some editing on the loaded image by adding text view and converted to image using RenderingTargetBitmap by and store each pixel in byte be calculating the image height like below.

    pixels.Length = 5184 x 3888 ;

    byte[] mergedPixels = new byte[pixels.Length * sizeof(int)]; //mergedPixels  = 80621568

    So, converting bytes to MB it crossed 80 MB and throwing insufficient memory exception. If I resized the same image to (4584, 3438), I can convert the edited image and save in my local machine.

    Please let me know any limitation on converting to image using Encoder.SetPixelData method.

    What's wrong with my code.


    Regards,

    Bharathi.





    Thursday, October 10, 2019 10:48 AM

All replies

  • Hi,

    Have you checked the current memory usage of your app then throwing the exception? You could try to use MemoryManager.AppMemoryUsage Property to get the app's current memory usage and MemoryManager.AppMemoryUsageLimit Property to the app's memory usage limit . Also please subscribe the MemoryManager.AppMemoryUsageIncreased Event to get informed when the memory usage increase. After you add them, please run your app again to see if the app memory is out of limits.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 11, 2019 3:23 AM
  • Hi,

    Thanks for your update. I have checked my app memory.

    App current memory usage = 34 MB on app starting                            

    When the app crashed with insufficient memory, increased to 703 MB in task manager.

    AppMemoryUsageLimit = 19622.793216 MB

    So, I suspect that the issue is not related to increasing app memory size. Is there anything wrong with my code or any memory limitation of using array of bytes in large amount (around 80MB) hence, getting this exception while calling the SetPixelData method?

    Thanks,

    Bharathi.

    Monday, October 28, 2019 7:10 PM
  • Hi,

    There is no limitation for the method in the document. Based on the memory the app using when crashed and the exception message, it seems the reason is that the available memory is not enough when then the time that your app is converting the image. The memory usage limit is the max value of the memory that your app could use. But the available memory doesn't equal to the usage limit. This will be affected by the state of the device. Do you have another device that has a smaller memory? When you run your app in that device,  what about the result when converting images?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, October 29, 2019 2:31 AM