Page 1 of 1

Display button (transparent) on VideoSourcePlayer

PostPosted: Fri Aug 09, 2019 4:23 pm
by dall
I want to display a button with transparent background (currently in a PictureBox) on the VideoSourcePlayer control like so:

screenshot.png
Screenshot of button on VideoSourcePlayer
screenshot.png (37.94 KiB) Viewed 1059 times

I can't get the button to be shown as transparent. I set up the PictureBoxes BackgroundColor to Color.Transparent and added the box to the player's list of controls. Trying to set the player's back color to transparent failed with an exception (not supported). If the player was another PictureBox it would have worked.

Any more ideas on what to try?
Thanks!

Re: Display button (transparent) on VideoSourcePlayer

PostPosted: Fri Aug 09, 2019 9:10 pm
by andrew.kirillov
Just make a custom drawing control and do whatever you would like (writing your code for drawing).

Any AForge.NET specific question ?

Re: Display button (transparent) on VideoSourcePlayer

PostPosted: Mon Aug 12, 2019 10:43 am
by dall
Hi,
thanks, did some more googling about that, tried it and it worked. Thought it was a VideoSourcePlayer related problem. I find the way WinForms is handling transparency very confusing.

I used this approach:
https://social.msdn.microsoft.com/Forum ... m=winforms

My class:
Code: Select all
public class TransparentPictureBox : Control
{
    private Timer refresher;
    private Image _image = null;
    private Image resizedImage = null;


    public TransparentPictureBox()
    {
        this.Cursor = Cursors.Hand;

        refresher = new Timer();
        refresher.Tick += new EventHandler(this.TimerOnTick);
        refresher.Interval = 50;
        refresher.Start();
    }


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


    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);

        if (_image != null)
            SetResizedImage();
    }


    private void SetResizedImage()
    {
        if (this._image != null)
        {
            if (resizedImage != null)
                resizedImage.Dispose();
            int newHeight = Helper.CalculateHeightForTargetWidth(_image.Size, this.Width);
            resizedImage = new Bitmap(_image, this.Width, newHeight);
        }
    }


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


    protected override void OnPaint(PaintEventArgs e)
    {
        if (resizedImage != null)
            e.Graphics.DrawImage(resizedImage, new Point(0, 0));
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
    }


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


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