- Decide on the Paper color
- Decide on a delta to allow the color to vary
- Decide on points along the bottom to do vertical tests
- Do the vertical tests going up, collecting the minimum y where the color stops appearing
- Do at least 10-20 such tests
The resulting y should be 1 more than what you want to keep. You may need to insert a limit to avoid cropping everything if the image is too bright. Either refine the algorithm or mark such an the image as an exception for manual treatment!
To crop you use the DrawImage
overload with source and dest rectangles!
Here are a few more hints:
To find the paper color you can go up right from the left bottom edge diagonally until you hit a pixel with a Color.GetBrightness
of > 0.8; then go further for 2 pixels to get clear of any antialiased pixels.
A reasonable delta will depend on your images; start with 10%
Use a random walk along the bottom; when you are done maybe add one extra pass in the close vicinity of the minimum found in pass one.
The vertical test can use GetPixel
to get at the colors or if that is too slow you may want to look into LockBits
. But get the search algorithm right first, only then think about optimizing!
If you run into trouble with your code, expand your question!