Jump to content
How to Install Plugins ×

Unselected Rectangle Keeper


Recommended Posts

This is a plugin I wrote a while ago, which I posted to a thread titled crop black and white images down to the smallest rectangular size that contains the black part.  Apparently inspired by the thread's title, I chose the rather awkward name Unselected Rectangle Keeper. It's in the Selection submenu.  It has no user interface.


This is the plugin: UnselectedRectangleKeeper.zip


The icon: UnselectedRectangleKeeper.png


What it does sounds kind of strange, but is useful for cropping images to the smallest region that doesn't match the surrounding color.


If you have a rectangular selection which contains a non-selected region, it erases everything in the selection that's outside the bounding rectangle of the non-selected region:


To crop to the smallest rectangle containing something not matching the color of the background:

Use the Magic Wand with a low tolerance to select the outside color.

Run Unselected Rectangle Keeper.


Use the Magic Wand to select the transparent region.

Invert the selection.

Crop to the selection.




Before (white region is selected).






The code:


// Author: MJW
// Name: Unselected Rectangle Keeper
// Title: Unselected Rectangle Keeper
// Submenu: Selection
// Desc: Erase the area outside the bounding rectangle of the unselected interior.
// Keywords: erase unselected region rectangle
#region UICode

private volatile bool firstTime = true;
private object myLock = new object();

int left, right, top, bottom;
void Render(Surface dst, Surface src, Rectangle rect)
    PdnRegion selectionRegion = EnvironmentParameters.GetSelection(src.Bounds);
    Rectangle selection = selectionRegion.GetBoundsInt();
    ColorBgra CurrentPixel;

    if (firstTime) lock (myLock)
        if (firstTime)
            int l = int.MaxValue, r = -1, t = int.MaxValue, b = -1;
            for (int y = selection.Top; y < selection.Bottom; y++)
                for (int x = selection.Left; x < selection.Right; x++)
                    if (!selectionRegion.IsVisible(x, y))
                        if (x < l)
                            l = x;
                        if (x > r)
                            r = x;
                        if (t == int.MaxValue)
                            t = y;
                        b = y;

            right = r; left = l; top = t; bottom = b;
            firstTime = false;

    // Erase everything outside the bounding rectangle for the unselected region.
    for (int y = rect.Top; y < rect.Bottom; y++)
        for (int x = rect.Left; x < rect.Right; x++)
            ColorBgra pixel;
            if ((y < top) || (y > bottom) || (x < left) || (x > right))
                pixel = ColorBgra.Transparent;
                pixel = src[x, y];
            dst[x, y] = pixel;



EDIT: Added link to plugin (which I completely forgot to do). Thanks for the reminder, lynxster4!

EDIT 2: Added the source code. (Using the "firstTime" flag is no longer necessary. CodeLab now supports PreRender.)


  • Upvote 4
Link to comment
Share on other sites

@MJW....maybe you should post the zip file above, so everyone doesn't have to go the link you provided?

They can go to the link for more information.  :)

  • Upvote 1
Link to comment
Share on other sites

  • 3 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...