Semi Transparent UserControl
-
Hi Guys Is it possible to create a usercontrol with a semi-transparent background (without affecting and not relying on the parent form’s properties). I can make the backcolor of the control transparent but I have to set the transparency key in the parent form for it to work but the form also renders out a transparent background :mad:!!!. Here is the code i'm using inside the Usercontrol :-D
private void DrawImageBox() { //Set Transparent Background ( Should be Semi-Transparent at 50% Opaque) SetStyle(ControlStyles.SupportsTransparentBackGround, true); base.BackColor = Color.Transparent; //Draw Image if image is set if (Picture != null) { Graphics gfx = base.CreateGraphics(); Bitmap bmp = new Bitmap(Picture); bmp.MakeTransparent( _transparentKey ); // Stretch the image to size of control Rectangle rec = Rectangle( 0, 0, base.Width, base.Height ); // Draw the image gfx.DrawImage( bmp, rec, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel); //Clean-up gfx.Dispose(); } }
If anyone has any ideas for how to do this I would be really grateful! Thanks in advance! :-D -
Hi Guys Is it possible to create a usercontrol with a semi-transparent background (without affecting and not relying on the parent form’s properties). I can make the backcolor of the control transparent but I have to set the transparency key in the parent form for it to work but the form also renders out a transparent background :mad:!!!. Here is the code i'm using inside the Usercontrol :-D
private void DrawImageBox() { //Set Transparent Background ( Should be Semi-Transparent at 50% Opaque) SetStyle(ControlStyles.SupportsTransparentBackGround, true); base.BackColor = Color.Transparent; //Draw Image if image is set if (Picture != null) { Graphics gfx = base.CreateGraphics(); Bitmap bmp = new Bitmap(Picture); bmp.MakeTransparent( _transparentKey ); // Stretch the image to size of control Rectangle rec = Rectangle( 0, 0, base.Width, base.Height ); // Draw the image gfx.DrawImage( bmp, rec, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel); //Clean-up gfx.Dispose(); } }
If anyone has any ideas for how to do this I would be really grateful! Thanks in advance! :-DHi, the short answer to your question is no. The win32 system does not support semi-transparent controls in the way you want it. However, considering that you're UserControl is painting an image, there is an alternative way - as long as you're only painting over other graphics & not over controls (buttons, textboxes etc.). Any image you want to paint can be adjusted so that it's semi-transparent. Ditch the UserControl & paint the image from the main form by overriding the OnPaint method. Here's the code to adjust the image transparency. You'll need to enable the 'Allow unsafe code' option in the project Build properties.
private Bitmap \_transImage = null; public Form1() { InitializeComponent(); } ~Form1() { \_transImage.Dispose(); } protected override void OnLoad(EventArgs e) { Bitmap b = new Bitmap(@"c:\\myImage.png"); \_transImage = AdjustImageTransparency(b); b.Dispose(); base.OnLoad(e); } protected override void OnPaint(PaintEventArgs e) { e.Graphics.DrawImage(\_transImage, new Rectangle(40, 40, \_transImage.Width, \_transImage.Height)); base.OnPaint(e); } private Bitmap AdjustImageTransparency(Bitmap sourceImage) { // image must be 32bpp to allow transparency adjustment, so paint source to transImage gc Bitmap transImage = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb); Rectangle rc = new Rectangle(Point.Empty, sourceImage.Size); using (Graphics g = Graphics.FromImage(transImage)) g.DrawImage(sourceImage, rc); // lock transImage for direct memory manipulation BitmapData bd = transImage.LockBits(rc, ImageLockMode.ReadWrite, transImage.PixelFormat); AdjustTransparency(bd, 0.5f); // must always unlock the memory when finished transImage.UnlockBits(bd); return transImage; } private unsafe void AdjustTransparency(BitmapData dataObj, float ratio) { // calc the number of bytes that represent a single row int byteWidth = dataObj.Width \* 4; for (int y = 0; y < dataObj.Height; y++) { // calc memory location for row(y) byte\* row = (byte\*)dataObj.Scan0 + dataObj.S