Click here to Skip to main content
15,936,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want show an image that contains png logo, this image must be transparent as it over another control (Camera Control).

What I have tried:

I tried this solution but the issue that the Image (in Transparent control) appears in back of the Camera Control, so how to bringTo front?

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace LiveQ.MrSViews.UserZControls
{
    public enum PictureBoxSizeMode
    {
        Normal,
        StretchImage,
        AutoSize
    }

    public partial class TransparentControl : UserControl
    {
        private readonly Timer refresher;
        private Image _image;
        
        public TransparentControl()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.Transparent;
            refresher = new Timer();
            refresher.Tick += TimerOnTick;
            refresher.Interval = 50;
            refresher.Enabled = true;
            refresher.Start();
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20; // WS_EX_TRANSPARENT
                return cp;
            }
        }

        protected override void OnMove(EventArgs e)
        {
            RecreateHandle();
        }

        private PictureBoxSizeMode _sizeMode = PictureBoxSizeMode.Normal;

        public PictureBoxSizeMode SizeMode
        {
            get { return _sizeMode; }
            set
            {
                _sizeMode = value;
                Invalidate(); // Invalidate the control to trigger a repaint
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (_image != null)
            {
                Rectangle destRect = new Rectangle(Point.Empty, this.Size);

                switch (_sizeMode)
                {
                    case PictureBoxSizeMode.Normal:
                        destRect = new Rectangle((Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2), _image.Width, _image.Height);
                        break;
                    case PictureBoxSizeMode.StretchImage:
                        destRect = new Rectangle(Point.Empty, this.Size);
                        break;
                    case PictureBoxSizeMode.AutoSize:
                        destRect = new Rectangle((Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2), _image.Width, _image.Height);
                        this.Size = new Size(_image.Width, _image.Height);
                        break;
                }

                e.Graphics.DrawImage(_image, destRect);
            }
        }
      
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            // Do not paint background
        }

        // Hack
        public void Redraw()
        {
            RecreateHandle();
        }

        private void TimerOnTick(object source, EventArgs e)
        {
            RecreateHandle();
            refresher.Stop();
        }

        public Image Image
        {
            get { return _image; }
            set
            {
                _image = value;
                RecreateHandle();
            }
        }
    }
}

also i tried:
// Bring TransparentControl to the front
           LogoControl.BringToFrontCustom();

           // Set z-index of TransparentControl
           LogoControl.SetZIndex(1);



public void BringToFrontCustom()
       {
           this.BringToFront();
       }

       public void SetZIndex(int index)
       {
           if (this.Parent != null && index >= 0 && index < this.Parent.Controls.Count)
           {
               this.Parent.Controls.SetChildIndex(this, index);
           }
       }
Posted

1 solution

This is a common question so I did a quick Google Search that many solutions for your question: winform trasparent image overlay[^]. Pick one that best suits your needs.
 
Share this answer
 

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