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 7934921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:46:06+00:00 2026-06-03T21:46:06+00:00

I need to calculate a close crop rectangle for a bitmap by a given

  • 0

I need to calculate a close crop rectangle for a bitmap by a given background color key. In the following pictures you can see what is meant to be the close crop. On the left side is the source, on the right side output of the close crop:

enter image description here

enter image description here

As you can see, I need to find the topmost, leftmost, lowermost and rightmost pixels which differ in color from the background to build the close crop rectangle. So, how to find those differing outer pixels to get the close crop rectangle ? Or, in other words, how to calculate close crop rectangle of a bitmap ?

  • 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-06-03T21:46:07+00:00Added an answer on June 3, 2026 at 9:46 pm

    You can use this code (you may follow the commented version of this post as well):

    procedure CalcCloseCrop(ABitmap: TBitmap; const ABackColor: TColor;
      out ACropRect: TRect);
    var
      X: Integer;
      Y: Integer;
      Color: TColor;
      Pixel: PRGBTriple;
      RowClean: Boolean;
      LastClean: Boolean;
    begin
      if ABitmap.PixelFormat <> pf24bit then
        raise Exception.Create('Incorrect bit depth, bitmap must be 24-bit!');
    
      LastClean := False;
      ACropRect := Rect(ABitmap.Width, ABitmap.Height, 0, 0);
    
      for Y := 0 to ABitmap.Height-1 do
      begin
        RowClean := True;
        Pixel := ABitmap.ScanLine[Y];
        for X := 0 to ABitmap.Width - 1 do
        begin
          Color := RGB(Pixel.rgbtRed, Pixel.rgbtGreen, Pixel.rgbtBlue);
          if Color <> ABackColor then
          begin
            RowClean := False;
            if X < ACropRect.Left then
              ACropRect.Left := X;
            if X + 1 > ACropRect.Right then
              ACropRect.Right := X + 1;
          end;
          Inc(Pixel);
        end;
    
        if not RowClean then
        begin
          if not LastClean then
          begin
            LastClean := True;
            ACropRect.Top := Y;
          end;
          if Y + 1 > ACropRect.Bottom then
            ACropRect.Bottom := Y + 1;
        end;
      end;
    
      if ACropRect.IsEmpty then
      begin
        if ACropRect.Left = ABitmap.Width then
          ACropRect.Left := 0;
        if ACropRect.Top = ABitmap.Height then
          ACropRect.Top := 0;
        if ACropRect.Right = 0 then
          ACropRect.Right := ABitmap.Width;
        if ACropRect.Bottom = 0 then
          ACropRect.Bottom := ABitmap.Height;
      end;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      R: TRect;
      Bitmap: TBitmap;
    begin
      CalcCloseCrop(Image1.Picture.Bitmap, $00FFA749, R);
      Bitmap := TBitmap.Create;
      try
        Bitmap.SetSize(R.Width, R.Height);
        Bitmap.Canvas.CopyRect(Rect(0, 0, R.Width, R.Height), Image1.Canvas, R);
        Image1.Picture.Bitmap.Assign(Bitmap);
      finally
        Bitmap.Free;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate the distance (in meters and miles) between two coordinates given
I need to calculate checksums of quite large files (gigabytes). This can be accomplished
I have an algorithm, and I need to calculate its complexity. I'm close to
I need to calculate the number of FULL month in SQL, i.e. 2009-04-16 to
I need to calculate date (year, month, day) which is (for example) 18 working
I need to calculate the within and between run variances from some data as
I need to calculate the quantity of nights (stay at a hotel) from the
I need to calculate permutations iteratively. The method signature looks like: int[][] permute(int n)
I need to calculate the timestamp of exactly 7 days ago using PHP, so
I need to calculate the age of a customer from their date of birth.

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.