Creating Non-Rectangular Custom Shaped Forms in C#
Custom shaped Windows Forms in any program are most commonly used for splash screens. However they are not limited to splash screens only, music players are a good example of that. For whatever use, the .Net Framework gives us two easy ways to achieve this effect, one using the Form property TransparencyKey and the other using the property Region.
Both methods require need a pre-created background image. You can choose to generate the image programmatically, but the whole point of this is to create stunning graphics which is easier to do with a good image editing program. Make sure your image has a contrasting background color. Remember, this is the color that will be filtered out, don’t make it similar to the rest of the image’s colors.
For the transparency method, simply:
1) Set the Form BackgroundImage to the image you created
2) Set the FormBorderStyle to None
3) Set the Form Width and Height to the BackgroundImage Width and Height
4) Set the Form TransparencyKey to Color.Fromargb(0,0,0) but replace 0,0,0 with your background color.
For the region method:
1) Set the Form BackgroundImage to the image you created
2) Create a new System.Drawing.Drawing2D.GraphicsPath object.
3) Go through each pixel of the BackgroundImage, for any pixel that does not match the background color (Color.Fromargb(#, #, #)), add a new rectangle to the GraphicsPath object. The rectangle will be created with new Rectangle(x, y, 1, 1), x and y being the coordinates of the given pixel.
4) Set the Form Region to new Region(GraphicsPath object here)