Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello morning everyone
anyone know how to crop (make a rectangular hole in) a control?

i try to crop a square in the middle of the panel
for an example
if the size of my panel was
C#

Panel myPanel = new Panel() { Size = new Size(300, 800), Location = new Point(0,0); BackColor = Color.FromArgb(0,0,0); }

i would like to crop a square with a size of 250, 250 and located at point 25,25

What I have tried:

C#
GraphicsPath graphicPath = new GraphicPath();
Rectangle myRec = new Rectangle(new point(25,25), new Size(250,250));
Panel myPanel = new Panel() { Size = new Size(300, 800), Location = new Point(0,0); BackColor = Color.FromArgb(0,0,0); }
myPanel.Region = new Region(myRec);


i know by doing this,
it just set the original panel to a new region.
but is there any method to crop the rectangle in the middle of panel..
thank you
Posted
Updated 1-Sep-17 13:33pm
v2
Comments
Graeme_Grant 31-Aug-17 23:40pm    
Are you trying to crop an image???
newbie1992 1-Sep-17 0:29am    
Hello sir
Nope.. i m not croping an image..

I try to crop my panel..so that there is an empty space in the middle of the panel
Graeme_Grant 1-Sep-17 0:31am    
Do you mean resize the panel?
newbie1992 1-Sep-17 1:57am    
Nope.. not resizebut i try to drill a 'hole' on the panel
The hole is in rectangle size..
It was like a card.. i draw a rectangle on it..
The i cut the rectangle..
I will get a card with a hole in the middle of the card
Ralf Meier 1-Sep-17 2:41am    
Sorry ... I didn't understand your issue.
Please try to explain it once more - perhaps improve your question with some pictures ...

In addition to the nice article mentioned by Graeme (which deserves points really) Making Transparent Controls - No Flickering[^] , the following:

If you want to "punch a hole", this article might be of interest: Phil Wright : Component Factory: .NET2, Transparent controls[^]
Also using WS_EX_TRANSPARENT might be of interest: c# - Any trick to use opacity on a panel in Visual Studio Window Form? - Stack Overflow[^]
 
Share this answer
 
v3
Comments
Graeme_Grant 1-Sep-17 7:39am    
5'd
[moved from comments - thanks for the suggestion RickZeeland]

Is the hole suppose to show objects behind it? Like a transparent hole? Something like this: Making Transparent Controls - No Flickering[^][^]???
 
Share this answer
 
Comments
RickZeeland 1-Sep-17 6:14am    
Your helping hand in CP land :)
Graeme_Grant 1-Sep-17 7:39am    
;)
using System.Drawing;
using System.Windows.Forms;

namespace PathStuff
{
    public static class PathExtensions
    {
        public static void MakeHole(this Control cntrl, Rectangle xrect)
        {
            var region = new Region(cntrl.ClientRectangle);
            region.Exclude(xrect);

            cntrl.Region = region;
        }

        public static void MakeHoleCenteredProportional(this Control cntrl, int scaleX, int scaleY)
        {
            var rect = new Rectangle();
            rect.Size = cntrl.ClientRectangle.Size;
            Region region = new Region(rect);

            rect.Inflate(cntrl.Width / (scaleX * -2), cntrl.Height / (scaleY * -2));

            region.Exclude(rect);

            cntrl.Region = region;
        }
    }
}
Sample uses in a Form:
using PathStuff;
namespace YourForm
{
    private void Form1_Load(object sender, EventArgs e)
    {
         PathExtensions.MakeHole(this, new Rectangle(100,100, 100, 100));

        // or
        PathExtensions.MakeHoleCenteredProportional(this Control cntrl, (this, 2, 2);
    
    }
};
Note the 'MakeHoleCenteredProportional method makes a hole centered in the Control; its size is determined by the ratios supplied: a value of #2 will create length, or width, of half the size of the corresponding dimension of the Control.

Suggestion: use double-buffering with Forms, or UserControls with this.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900