# Make a Region from non-transparent pixels in C# The example Make shaped PictureBoxes in C# sets the Region property of two PictureBox controls to circular regions. That works well in that example where the images are round volleyballs with transparent backgrounds, but it would be hard to manually create regions representing more irregular areas such as the ones used by this example.

This example uses the following method to automatically create a Region representing the non-transparent pixels in a bitmap.

```// Make a region representing the
// image's non-transparent pixels.
public static Region MakeNonTransparentRegion(Bitmap bm)
{
if (bm == null) return null;

// Make the result region.
Region result = new Region();
result.MakeEmpty();

Rectangle rect = new Rectangle(0, 0, 1, 1);
bool in_image = false;
for (int y = 0; y < bm.Height; y++)
{
for (int x = 0; x < bm.Width; x++)
{
if (!in_image)
{
// We're not now in the non-transparent pixels.
if (bm.GetPixel(x, y).A != 0)
{
// We just started into non-transparent pixels.
// Start a Rectangle to represent them.
in_image = true;
rect.X = x;
rect.Y = y;
rect.Height = 1;
}
}
else if (bm.GetPixel(x, y).A == 0)
{
// We are in the non-transparent pixels and
// have found a transparent one.
// Add the rectangle so far to the region.
in_image = false;
rect.Width = (x - rect.X);
result.Union(rect);
}
}

// Add the final piece of the rectangle if necessary.
if (in_image)
{
in_image = false;
rect.Width = (bm.Width - rect.X);
result.Union(rect);
}
}

return result;
}```

This method first makes an empty Region. It then loops through each row in the bitmap. It scans each row building horizontal strips of non-transparent pixels and adds a rectangle to the region representing each strip.

This method is relatively simple and handles odd cases such as images that include multiple disconnected non-transparent areas or areas with transparent holes. This entry was posted in algorithms, geometry, graphics, image processing and tagged , , , , , , , , , , , , , . Bookmark the permalink.

### 1 Response to Make a Region from non-transparent pixels in C#

1. Buldron says:

Love the tutorial, you’ve saved me a lot of time and taught me a lot.
Appreciate it.

This site uses Akismet to reduce spam. Learn how your comment data is processed.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7382104,"math_prob":0.9504307,"size":1937,"snap":"2020-45-2020-50","text_gpt3_token_len":450,"char_repetition_ratio":0.14485256,"word_repetition_ratio":0.0124223605,"special_character_ratio":0.26226124,"punctuation_ratio":0.16755319,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9665287,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12],"im_url_duplicate_count":[null,3,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-11-27T16:46:53Z\",\"WARC-Record-ID\":\"<urn:uuid:b7b9c8f0-c537-4291-a701-dbdb9542d1c3>\",\"Content-Length\":\"50945\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:96a8856e-7def-4c02-a732-2da70dc3691d>\",\"WARC-Concurrent-To\":\"<urn:uuid:cc9dfe20-da28-41bf-ae03-34917d88c8db>\",\"WARC-IP-Address\":\"107.180.57.98\",\"WARC-Target-URI\":\"http://csharphelper.com/blog/2016/09/make-a-region-from-non-transparent-pixels-in-c/\",\"WARC-Payload-Digest\":\"sha1:IET4HAP4VOS7ILKSDORSFWJHCJN6CAMH\",\"WARC-Block-Digest\":\"sha1:HCNTMCMDCVUWZ5IAFLH5JPYAL7G5OC4E\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141193856.40_warc_CC-MAIN-20201127161801-20201127191801-00323.warc.gz\"}"}