Hi!

I have a grayscale image in my pictureBox. I want to box up the white pixels in the image using the rectangle. Can anyone teach me how to write the code. Thanks in advance ..

Best regards,

Dani AI

Generated

If by "box up" you mean draw rectangles around white areas, you first need to decide what counts as white. In real images you rarely get pure 255; pick a threshold (for example, pixels with luminance >= 240). Also, small correction to : in WinForms the origin is top-left (0,0), X increases to the right, Y increases downward. is right that white areas are often irregular, so you will usually want one rectangle per connected white region.

Quick start: draw one rectangle around all white pixels (good first check that your thresholding works).

// using System.Drawing; using System.Windows.Forms;
void BoxAllWhite(PictureBox pb, byte threshold = 240)
{
    var bmp = (Bitmap)pb.Image;
    int minX = bmp.Width, minY = bmp.Height, maxX = -1, maxY = -1;

    for (int y = 0; y < bmp.Height; y++)
    for (int x = 0; x < bmp.Width; x++)
    {
        Color c = bmp.GetPixel(x, y); // OK for small images; use LockBits for speed
        int lum = (299 * c.R + 587 * c.G + 114 * c.B) / 1000; // ITU-R BT.601
        if (lum >= threshold)
        {
            if (x < minX) minX = x; if (y < minY) minY = y;
            if (x > maxX) maxX = x; if (y > maxY) maxY = y;
        }
    }

    if (maxX >= 0)
    {
        using (var g = Graphics.FromImage(bmp))
        using (var pen = new Pen(Color.Red, 1))
            g.DrawRectangle(pen, Rectangle.FromLTRB(minX, minY, maxX + 1, maxY + 1));
        pb.Image = bmp;
    }
}

To draw a rectangle per white area: threshold once into a boolean mask, then run a flood-fill (BFS/DFS) to label connected components (4- or 8-neighborhood). For each component, track minX/minY/maxX/maxY while visiting pixels and draw that rectangle. Use Bitmap.LockBits + Marshal.Copy for speed; GetPixel will be slow on large images. Finally, if pictureBox.SizeMode != Normal, either draw on the bitmap itself (as above) or map coordinates between image and control to avoid misaligned boxes. Adjust the threshold if compression noise or antialiasing leaves near-white edges, as anticipated.

Recommended Answers

All 6 Replies

What do you mean by I want to box up the white pixels in the image using the rectangle. ?
Coordinates start in the upper left corner with (0,0).
Y coordinates increase by moving down.
X coordinates increase by moving left.

Do you mean you want to draw a rectangle around area that have all-white pixels? This will work OK if the areas are in fact only rectangular but I highly suspect you will find jagged edges with white pixels. How should that be handled?

What do you mean by I want to box up the white pixels in the image using the rectangle. ?
Coordinates start in the upper left corner with (0,0).
Y coordinates increase by moving down.
X coordinates increase by moving left.

Thanks for the reply and apologize for my poor English language :)

Box up mean indicate with rectangles where the white pixels of the image are.

I have a gray-scale image in the pictureBox and I want to draw a rectangle on the white pixels of the image. Now my problem is i don't know how to get the X-Y coordinate and size of the white pixels of the image for drawing the rectangle on them. As i m a beginer in programming, could you guide me how can i write it in c#. Thanks in advance.

best regards,

Check out this thread:
http://www.daniweb.com/forums/thread215149.html

It does basically the same thing but uses a screen shot instead of a bitmap file. You can just change that section of the code out then it shows you how to find a pixel. After you have a working version of that code then post it up here and we'll go from there!

Do you mean you want to draw a rectangle around area that have all-white pixels? This will work OK if the areas are in fact only rectangular but I highly suspect you will find jagged edges with white pixels. How should that be handled?

Yes, I want to draw rectangles around area that have all-white pixels of the image. The white pixels will be within the rectangle. How can i write it in c#. Thanks a lot for the reply.

Best Regard,

Upload your sample image

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.