Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7188295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:02:52+00:00 2026-05-28T19:02:52+00:00

What I am trying to do is check an image row or column, and

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:02:53+00:00Added an answer on May 28, 2026 at 7:02 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to check in jQuery if a div contains some text, and then
I'm trying to make a checkbox that has a custom check image. I need
I'm trying to check the page and find the divs that do not have
I am trying to add a magic fields plugin image code that will check
I am trying to resize an image and then display it to check whether
I am using this code to display image from URL, but it is not
So what I am trying to create is an activity that displays an image
I am trying to check if a file is an image before I upload
i am trying to check if user uploaded his or her avatar image and
I am trying to update a GtkImage with gtk_image_set_from_file(), but how can I check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.