1

I have tried .tif and .tiff images to load in a custom controls picturebox, and I'm looping it through many images (about 50 or so) and I keep get the out of memory exception error. I have tried to use .tif or .tiff for it because I have heard, that .tif is smaller than .tiff. Also, the .png format does not help at all as well. I can still change my formats but I need to keep my alpha channel.

This is a part of my code:

foreach (CategoryDTO category in Categories)
{
    foreach(The_Forest_App.JsonObjects.Item item in category.Items)
    {
        string filepath = "res/img/items/" + Language + "/" + item.Name.Replace(" ", string.Empty).ToLower() + ".png";

        The_Forest_App.Item ItemListed = new The_Forest_App.Item
                {
                    ItemName = item.Name,
                    ItemImage = Image.FromFile(filepath),
                    Parent = itemsFlowLayoutPanel
                };
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Proudyy
  • 57
  • 5
  • [This related post](https://stackoverflow.com/a/3848178/1911064) claims that `out of memory` exceptions can also be thrown for invalid file formats. – Axel Kemper Aug 23 '20 at 20:47
  • 2
    Could this actually be a resource depletion issue? – Loren Pechtel Aug 23 '20 at 20:51
  • Well my file format can't be invalid because I can easily convert it or open it and view the image without any issues. – Proudyy Aug 23 '20 at 21:08
  • You keep overwriting `ItemListed` but never do anything with it. What's the use of this code? – Gert Arnold Aug 23 '20 at 21:52
  • First thing you'd do is use a `Dictionary` so you can be sure a bitmap is loaded only once. Next thing you'd do is generate an image that fits the picturebox, so it is much smaller and renders a lot faster. Next thing you'd do is Project > Properties > Compile tab, untick "Prefer 32-bit", you don't prefer it when you run out of memory. – Hans Passant Aug 23 '20 at 22:42

0 Answers0