Remove the black border around the image

I have several jpg images. Some of them may have a black border on one or more sides, and I would like to remove them. The black frame cannot go around the actual image - some can only have a border at the bottom (with the actual image at the top), while some can be centered (which means that the black edges are on both sides, but not connected). Worse, images are compressed with JPG, so they may not be exactly 0.0.0 black.

In Paint, I would just use a low-tolerance Magic Wand tool, but I need to do this in C # on ASP.net, and I don't know what is the best way to do this ..

Should I “scan” each row and then each column (two nested loops) to find black areas? Sounds a little silly to do, performance and CPU loading. Or does GDI + have a built-in magic wand tool?

Images are not that big (maximum 474x474 pixels) and are cached afterwards, but I need to keep server load as little as possible.

Any hints that the least stupid way to do this would be?

+5
source share
1 answer

It seems that for each edge you can do something like this:

for each edge:
    for (i = 0; ; i++) {
         compute average pixel value along edge row/column + i
         if (average value > threshold)
              break;
    }
    crop image
+4
source

All Articles