public Bitmap SubImage(Bitmap source, int x, int y, int width, int height)
{
//// test if first column only contains very few whites, then ommit this column
//if (height > 7) // TODO this extra check is better for 'a', but worse for 'l'
//{
// int firstWhites = 0, minNeeded = 2;
// for (int i = 0; i < height; i++)
// if (source.GetPixel(x, y + i).R != 0)
// {
// firstWhites++;
// if (firstWhites > minNeeded)
// break;
// }
// if (firstWhites <= minNeeded)
// {
// //// draw uncropped
// //Rectangle cropRectL = new Rectangle(x, y, width, height);
// //Bitmap targetL = new Bitmap(cropRectL.Width, cropRectL.Height);
// //using (Graphics g = Graphics.FromImage(targetL))
// //{
// // g.DrawImage(source, new Rectangle(0, 0, targetL.Width, targetL.Height),
// // cropRectL,
// // GraphicsUnit.Pixel);
// //}
// //targetL.Save("D:\\temp\\debug_uncroppedLetter.png");// TODO comment out
// x++;
// width--;
// }
//}
Rectangle cropRect = new Rectangle(x, y, width, height);
Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
using (Graphics g = Graphics.FromImage(target))
{
g.DrawImage(source, new Rectangle(0, 0, target.Width, target.Height),
cropRect,
GraphicsUnit.Pixel);
}
return target;
}