Jeremy Styers
Jeremy Styers

Reputation: 520

Why doesn't c# bitmap setPixel() method work?

I have a method that takes in numbers for a rectangle and "blanks" the pixels (turns them white) for that area. For some reason though when my program searches for non white pixels, its finding the ones i just set to white.

Why is it saying its not white when it is? I have an image that gets cropped and is saved to my hard drive. So I'm able to view the area it says isn't white, but when i open the image, its completely white as can be. So I'm at a lose as to why this isn't working. The program stops on the same pixel every time. It says the R value is 238 and I know that pixel was set to white because i stepped through the debugger and watched the pixel value go into the bmp.SetPixel method.

This is the code for the "blanking" method:

void blankArea(int x, int y, int width, int height)
        {
            for (int i = y; i <= height+y; ++i)
                for (int t = x; t <= width+x; ++t)
                {
                    if (t == w)
                        break;
                    bmp.SetPixel(t, i, Color.White);
                }
        }

this is the code that says that pixel isn't white:

bool allWhiteColumn (int col)
        {
            for (int i = 5; i < h; ++i)
                if (bmp.GetPixel(col - 1, i).R < 248 && bmp.GetPixel(col - 1, i).G < 248)
                {
                    imageBelowColumEnd = bmp.GetPixel(col - 1, i).R;
                    this.row = i;
                    return false;
                }
            return true;
        }

Any help at this point would be greatly appreciated. I have no idea why it's saying the R is 238 after I set it to white. Thanks

EDIT- since I post a comment I'll edit instead. Taking the if out, did nothing. Same problem. I can't check for plain white. The program finds images from a scanner. So because a scanners "white" areas are not true white, I can not look for Color.WHITE. I'll try the bitlock though...

EDIT 2- "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." was thrown when i tried to do the example for the locking and unlocking of the bits. It threw it once it tried to copy the bits into the array. Apparently thats not allowed.

Thanks for the answers though.

Upvotes: 0

Views: 3779

Answers (3)

Ian
Ian

Reputation: 34549

Would it not make more sense to check for white for starters?

bool allWhiteColumn (int col)
{
    for (int i = 5; i < h; ++i)
        if (bmp.GetPixel(col - 1, i) != Color.White)
        {
            imageBelowColumEnd = bmp.GetPixel(col - 1, i).R;
            this.row = i;
            return false;
        }
    return true;
}

Upvotes: 0

jdehaan
jdehaan

Reputation: 19938

Remove the if (t == w) break; in your first function, whatever is w (which you did not describe). I guess the code breaks and you end up with an incomplete white region.

Upvotes: 1

neeKo
neeKo

Reputation: 4280

You could try to use the Bitmap.Lockbits() method before, and Bitmap.Unlockbits() after you have changed the pixels.

Actually, here is an example: http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx

Upvotes: 2

Related Questions