Search code examples
c#imageimage-processingimage-manipulationimage-resizing

How to resize an image in C# to a certain hard-disk size?


How to resize an image an image in C# to a certain hard-disk size, like 2MiB? Is there a better way than trial and error (even if it's approximate, of course).

Any particular keywords to search for when trying to find the solution on the web?


Solution

  • It depends on what you are willing to change

    1. Make the size of the image smaller
    2. Change the format of the image
    3. If the format supports a lossy compression, decrease the quality
    4. If you are storing meta-data that you don't need, remove it
    5. Reduce the number of colors (and bits per pixel)
    6. Change to a paletted format
    7. Change to a paletted format and reduce the colors

    It's hard to guess what the final disk size will be, but if you know a starting point you can get a pretty good estimate. Reducing the size will probably be proportional, reducing the bits per pixel will also likely be proportional.

    If you change the format, compression or quality, it's really just a guess -- depends highly on the image content. You could probably get a good range by trying it on a corpus of images that matches what you think you'll be seeing.