This is a migrated thread and some comments may be shown as answers.

Crop white space around picture in pictuebox

2 Answers 6 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Lastos
Top achievements
Rank 1
Lastos asked on 15 Nov 2016, 11:01 AM
How do i assign the value of a picture box to a Bitmap  once it has been retrieved from dataset . I need to crop the white space around it

2 Answers, 1 is accepted

Sort by
0
Lastos
Top achievements
Rank 1
answered on 15 Nov 2016, 11:14 AM

This is the function code I want to use to against a pictorebox1.Value

    public static Bitmap Crop(Bitmap bmp)
        {
            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);
            }
        }

0
Stef
Telerik team
answered on 15 Nov 2016, 04:02 PM
Hello Lastos,

With consideration of the supported types for a PictureBox.Value, you can create a user function returning the image.

The code modifying the image can be executed in the user function, or if you prefer you can only get the already modified and saved image in the function.

Regards,
Stef
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Lastos
Top achievements
Rank 1
Answers by
Lastos
Top achievements
Rank 1
Stef
Telerik team
Share this question
or