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

  • Home
  • SEARCH
  • 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 124659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:02:07+00:00 2026-05-11T05:02:07+00:00

I am looking for a solution for detecting edge whitespace of c# bitmap, from

  • 0

I am looking for a solution for detecting edge whitespace of c# bitmap, from the c# managed GDI+ library.

The images would be either transparent or white, most of the 400x pictures are 8000x8000px with about 2000px whitespace around the edges.

What would be the most efficient way of finding out the edges, x, y, height and width coordinates? I tried a go pixel by pixel but was finding it very slow.

Update to solution –Added left/right/top/bottom bounds

Problems with images detail center images, now crops any transparent (0%) or white (#FFFFFF) pixels.

var top = bitmap.Height; var left = bitmap.Width; var right = 0; var bottom = 0; 

…

var pData = pData0 + (y * data.Stride) + (x * 4); var xyAlpha = pData[3]; var xyBlue = pData[0]; var xyGreen = pData[1]; var xyRed = pData[2]; if ((xyAlpha > 0) || (xyRed != 255 && xyGreen != 255 && xyBlue != 255)) {     if (y < top)         top = y;     if (y > bottom)         bottom = y;     if (x < left)         left = x;     if (x > right)         right = x; } 

…

var cropWidth = right - left; var cropHeight = bottom - top; var cropX = top; var cropY = left;  var cacheBitmap = new Bitmap(cropWidth, cropHeight, PixelFormat.Format32bppArgb); using (var cacheGraphics = Graphics.FromImage(cacheBitmap)) {     cacheGraphics.DrawImage(context.Image, new Rectangle(0, 0, cropWidth, cropHeight), cropX, cropY, cropWidth, cropHeight, GraphicsUnit.Pixel); } 
  • 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. 2026-05-11T05:02:08+00:00Added an answer on May 11, 2026 at 5:02 am

    A great GDI+ resource is Bob Powell’s GDI+ FAQ!

    You didn’t say how you accessed the pixels in the image so I will assume that you used the slow GetPixel methods. You can use pointers and LockBits to access pixels in a faster way: see Bob Powells explanation of LockBits

    • This will require an unsafe code block – if you don’t want this or you do not have FullTrust you can use the trick explained here: Pointerless Image Processing in .NET by J. Dunlap

    The below code uses the LockBits approach (for the PixelFormat.Format32bppArgb) and will fill the start and end Points with the value where the first and last pixels in an image are discovered that do not have the color described in the argument color. The method also ignores completely transparent pixels which is useful if you want to detect the area of an image where the visible ‘content’ starts.

        Point start = Point.Empty;     Point end = Point.Empty;          int bitmapWidth = bmp.Width;     int bitmapHeight = bmp.Height;          #region find start and end point     BitmapData data = bmp.LockBits(new Rectangle(0, 0, bitmapWidth, bitmapHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);     try     {         unsafe         {             byte* pData0 = (byte*)data.Scan0;             for (int y = 0; y < bitmapHeight; y++)             {                 for (int x = 0; x < bitmapWidth; x++)                 {                     byte* pData = pData0 + (y * data.Stride) + (x * 4);                          byte xyBlue = pData[0];                     byte xyGreen = pData[1];                     byte xyRed = pData[2];                     byte xyAlpha = pData[3];                               if (color.A != xyAlpha                             || color.B != xyBlue                             || color.R != xyRed                             || color.G != xyGreen)                     {                         //ignore transparent pixels                         if (xyAlpha == 0)                             continue;                         if (start.IsEmpty)                         {                             start = new Point(x, y);                         }                         else if (start.Y > y)                         {                             start.Y = y;                         }                         if (end.IsEmpty)                         {                             end = new Point(x, y);                         }                         else if (end.X < x)                         {                             end.X = x;                         }                         else if (end.Y < y)                         {                             end.Y = y;                         }                     }                 }             }         }     }     finally     {         bmp.UnlockBits(data);     }     #endregion 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 81k
  • Answers 81k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have been keeping my eye on this one: core-plot… May 11, 2026 at 4:30 pm
  • Editorial Team
    Editorial Team added an answer Here's an example that does what I think you're trying… May 11, 2026 at 4:30 pm
  • Editorial Team
    Editorial Team added an answer I think the critical difference between the two scenarios is… May 11, 2026 at 4:30 pm

Related Questions

I've seen a few questions here related to determining the similarity of files, but
I have this java swing application that I intend to sell over the internet.
I am looking for a backend solution for an application written in Ruby on
Thanks for the three excellent answers which all identified my problem of using "onclick

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.