0

I have a bitmap image from which i trim whitespaces around the image but now image have its coordinate like (40,20) and i want the image should have (0,0) coordinates. How this can be done .

For triming white space i used following Code:

 private static Bitmap ImageTrim(Bitmap bmp)
    {
        //get image data
        int w = bmp.Width;
        int h = bmp.Height;

        Func<int, bool> allWhiteRow = row =>
        {
            for (int i = 0; i < w; ++i)
                if (bmp.GetPixel(i, row).R != 255)
                    return false;
            return true;
        };

        Func<int, bool> allWhiteColumn = col =>
        {
            for (int i = 0; i < h; ++i)
                if (bmp.GetPixel(col, i).R != 255)
                    return false;
            return true;
        };

        int topmost = 0;
        for (int row = 0; row < h; ++row)
        {
            if (allWhiteRow(row))
                topmost = row;
            else break;
        }

        int bottommost = 0;
        for (int row = h - 1; row >= 0; --row)
        {
            if (allWhiteRow(row))
                bottommost = row;
            else break;
        }

        int leftmost = 0, rightmost = 0;
        for (int col = 0; col < w; ++col)
        {
            if (allWhiteColumn(col))
                leftmost = col;
            else
                break;
        }

        for (int col = w - 1; col >= 0; --col)
        {
            if (allWhiteColumn(col))
                rightmost = col;
            else
                break;
        }

        if (rightmost == 0) rightmost = w; // As reached left
        if (bottommost == 0) bottommost = h; // As reached top.

        int croppedWidth = rightmost - leftmost;
        int croppedHeight = bottommost - topmost;

        if (croppedWidth == 0) // No border on left or right
        {
            leftmost = 0;
            croppedWidth = w;
        }

        if (croppedHeight == 0) // No border on top or bottom
        {
            topmost = 0;
            croppedHeight = h;
        }

        try
        {
            var target = new Bitmap(croppedWidth, croppedHeight);
            using (Graphics g = Graphics.FromImage(target))
            {
                g.DrawImage(bmp,
                  new RectangleF(0, 0, croppedWidth, croppedHeight),
                  new RectangleF(leftmost, topmost, croppedWidth, croppedHeight),
                  GraphicsUnit.Pixel);
            }
            return target;
        }
        catch (Exception ex)
        {
            throw new Exception(
              string.Format("Values are topmost={0} btm={1} left={2} right={3} croppedWidth={4} croppedHeight={5}", topmost, bottommost, leftmost, rightmost, croppedWidth, croppedHeight),
              ex);
        }
    }

I checked Crop image white space in C# but if i take input image with (40,20) coordinate it doesnt give output image at (0,0) coordinate.

Community
  • 1
  • 1
User18
  • 101
  • 1
  • Possible duplicate of [Crop image white space in C#](http://stackoverflow.com/questions/16583742/crop-image-white-space-in-c-sharp) – MikeJRamsey56 Dec 14 '16 at 05:53
  • I tried you code and it positions the image at 0,0 for me. The only issue I found is if the white border is around an image with a white background it chops part of the image off. (Try drawing random shapes in Paint and see what I mean) – Troy Mac1ure Dec 14 '16 at 06:05
  • @MikeJRamsey : i tried the code it trim white spaces but coordinate system remain unchanged.. if i take input image with coordinate system (0,0) it works perfectly as output will also be at (0,0) but my input image is not at (0,0). – User18 Dec 14 '16 at 06:42
  • @Troy Mac1ure : u have taken your input image which itself is in (0,0) coordinate system .but if i take (40,20) at initial input then it doesnt return output with (0,0) – User18 Dec 14 '16 at 06:43
  • There __is no such thing__ as a 'coordinate system' to a Bitmap. It always starts at (0,0) and always goes to (width-1, height-1). As written the questions make no sense, so please explain better what you mean. Are you talking about the actual image or about what you maybe see in a PictureBox? At which SizeMode? – TaW Dec 14 '16 at 07:10

0 Answers0