[Solved] Cropping empty space around drawing [modified]
-
I have series of images very similar to this example image: http://img258.imageshack.us/img258/2483/exampleep8.gif[^] As you can see there's empty white space around the text and lines. How can I remove this empty space and crop the image? The red line shows where I want to crop it. I'm a newbie when it comes to Drawing/GDI+, I appreciate any help.
modified on Wednesday, June 11, 2008 8:25 AM
-
I have series of images very similar to this example image: http://img258.imageshack.us/img258/2483/exampleep8.gif[^] As you can see there's empty white space around the text and lines. How can I remove this empty space and crop the image? The red line shows where I want to crop it. I'm a newbie when it comes to Drawing/GDI+, I appreciate any help.
modified on Wednesday, June 11, 2008 8:25 AM
Problem solved here's the code:
Bitmap b = new Bitmap(filename);
Rectangle r = new Rectangle();
int topLeft = b.Width, topRight = 0, topUpper = b.Height, topBottom = 0;
string whitePixel = "ffffffff";for (int y = 1; y < b.Height; y++)
{
for (int x = 1; x < b.Width; x++)
{
if (b.GetPixel(x, y).Name != whitePixel)
{
if (x < topLeft)
topLeft = x;if (x > topRight)
topRight = x;if (y < topUpper)
topUpper = y;if (y > topBottom)
topBottom = y;
}
}
}r.Location = new Point(topLeft-1, topUpper-1);
r.Size = new Size((topRight - topLeft)+1, (topBottom - topUpper)+1);
Bitmap b2 = b.Clone(r, b.PixelFormat);
b2.Save(newfilename, ImageFormat.Gif);