C # transparent background Panel, transparent image, and PitureBox transparent Effect
1. Custom transparent background Panel control: Add TransparentPanel. cs class to the Project
using System.Windows.Forms;using System.Drawing;
Public class TransparentPanel: Control {public TransparentPanel () {} protected override void OnPaintBackground (PaintEventArgs e) {// do not draw the background} protected override CreateParams {get {CreateParams cp = base. createParams; cp. exStyle | = 0x00000020; // WS_EX_TRANSPARENT return cp ;}} protected override void OnPaint (System. windows. forms. paintEventArgs e) {// draws the background image of the panel if (BackgroundImag E! = Null) e. graphics. drawImage (this. backgroundImage, new Point (0, 0);} // Add the custom property value num1 for the control // private int num1 = 1; // [Bindable (true ), category ("custom attribute bar"), DefaultValue (1), Description ("here is the Description of the custom attribute Attr1! ")] // Public int Attr1 // {// get {return num1 ;}// set {this. Invalidate ();}//}}
2. After F5 is compiled and run once, you can find the control TransparentPanel In the toolbar.
Then add the control to the Form and set its Image attribute to the *. png Image with transparency information to see the transparency effect.