Earlier versions of this were called "MaskoMania" but a lot has been cleaned up and additional functionality added.
Basically it is a UserControl to be hosted within a container control with a bitmap image. The user can "lasso" a rectangle to be cut or copied, dragged then pasted.
Code:
'Mouse Actions:
'-------------
'
'When Enabled = True the mouse performs the following actions. When Enabled = False
'the events pass through with no processing.
'
'On the container:
'
' Left-click to clear the selection.
'
' Left-drag then release to establish the selection and copy or cut from the
' container's bitmap. See the Cutting property. A cut operation backfills with
' the MaskColor. A very small drag acts like a click.
'
' Shift-left-click makes Masking = False.
'
' Shift-right-click to choose the color under the mouse as MaskColor and make
' Masking = True.
'
'On the control:
'
' Left-drag to move the control.
'
' Shift-left-click makes Masking = False.
'
' Right-click pastes the contents of the control into the container bitmap.
'
' Shift-right-click to choose the color under the mouse as MaskColor and make
' Masking = True.
Masking is used to designate a transparent color. Minimalist demo:
There is also another demo in the attachment. It covers a wider set of features available, like passing a snippet from one container to another or loading an external file as a snippet.
I suppose an enhancement might be an optional feature to collapse the selection bounding rectangle to minimum size automatically when there is a mask (transparent) color. I'm not sure whether there might be GDI calls to expedite that.
Ok, got bored and implemented this new AutoCrop property. Sort of by brute force, but performance seems adequate.
Here I set AutoCrop = True at design time:
Remember, it crops away the MaskColor (down to a smaller rectangle) so you'll want to set a MaskColor even if you set Masking = False.
I've included only the UserControl itself here. Just replace it in the demo projects above for a quick test. Be sure to add a button to choose/clear AutoCrop, it defaults to False.
Oops! Introduced a bug. Now fixed and reposted.
Updated again with corrections for containers using aberrant ScaleMode settings.
Last edited by dilettante; Aug 28th, 2020 at 01:49 PM.
When you get something from the left panel, and mouse pointer goes out of that panel then an error occured. Width and Height of the area has to be checked before the copy actual happen.
Subscript out of range
ExitLeft:
If X < .Right Then .Left = X
For Y = .Top To .Bottom
For X = .Left To .Right
If PixelValues(X, Y) <> MaskColorRGB Then GoTo ExitTop
Next
Next