sjensen85
sjensen85

Reputation: 150

C# Bitmap difficulties

What I am trying to do is check an image row or column, and if it contains all white pixels then trim that row or column. I am not sure why, but when I run this code snippet, img.Width in TrimLeft is 1, before subtracting 1 from it. The actual image width is 175. I end up with an ArgumentException. I have a similar method for trimming the right side, and that works fine.

class ImageHandler
{

    public Bitmap img;
    public List<int[]> pixels = new List<int[]>();

    public ImageHandler(String path)
    {
        img = new Bitmap(path);
        GetPixels();
    }

    public void TrimLeft()
    {
        while (CheckColIfWhite(0, 0))
        {
            Rectangle cropBox = new Rectangle(1, 0, (img.Width-1), img.Height);
            Bitmap cropdImg = CropImage(img, cropBox);
            img = cropdImg;
        }
    }

    public bool CheckColIfWhite(int colx, int starty)
    {
        bool allPixelsWhite = false;
        int whitePixels = 0;

        for (int y = starty; y < img.Height; y++)
        {
            if (pixels[y][colx] >= 200) { whitePixels++; }
            else { return false; }
            if (whitePixels == img.Height) { allPixelsWhite = true; }
        }
        return allPixelsWhite;
    }

    public void GetPixels()
    {
        for (int y = 0; y < img.Height; y++)
        {
            int[] line = new int[img.Width];
            for (int x = 0; x < img.Width; x++)
            {
                line[x] = (int) img.GetPixel(x, y).R;
            }
            pixels.Add(line);
        }
    }

    public Bitmap CropImage(Bitmap tImg, Rectangle area) 
    {
        Bitmap bmp = new Bitmap(tImg);
        Bitmap bmpCrop = bmp.Clone(area, bmp.PixelFormat);
        return bmpCrop;
    }
}

Upvotes: 0

Views: 399

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239636

Your method seems like it will be remarkably inefficient - if the image is 175 pixels wide, and entirely white, you're going to create 175 copies of it, before (probably) failing when trying to create a 0 pixel wide image.

Why not examine each column in turn until you find a non-white column, and then perform a single crop at that time. Untested code, and with other changes hopefully obvious:

public Bitmap CropImage (Bitmap image)
{
    int top = 0;
    int bottom = image.Height-1;
    int left = 0;
    int right = image.Width-1;
    while(left < right && CheckColIfWhite(image,left))
         left++;
    if(left==right) return null; //Entirely white
    while(CheckColIfWhite(image,right)) //Because left stopped, we know right will also
         right--;
    while(CheckRowIfWhite(image,top))
         top++;
    while(CheckRowIfWhite(image,bottom))
         bottom--;
    return CropImage(image,new Rectangle(left,top,right-left+1,bottom-top+1));
}

(E.g. I'm now passing the image around, I've modified CheckColIfWhite and CheckRowIfWhite to take the image also, and to assume that one parameter is always fixed at 0)


Also, not sure why you're extracting the pixel array beforehand, so I'm putting my re-written CheckColIfWhite too:

public bool CheckColIfWhite(Bitmap image,int colx)
{
    for (int y = 0; y < image.Height; y++)
    {
        if (image.GetPixel(colx,y).R < 200)
            return false;
    }
    return true;
}

Upvotes: 2

Related Questions