Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. Semi Transparent UserControl

Semi Transparent UserControl

Scheduled Pinned Locked Moved Graphics
graphicstutorial
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maxatlis
    wrote on last edited by
    #1

    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

    R 1 Reply Last reply
    0
    • M maxatlis

      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

      R Offline
      R Offline
      Rob Smiley
      wrote on last edited by
      #2

      Hi, 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
      
      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups