Recently I have had to utilize transparent images within Panel(s) and PictureBox(s) for the wiindows app.
However it seems that neither will not do true transparency because each clears it’s background using the Windows Forms transparency scheme, thus resulting in the background color of any parent elements being rendered.
All is not lost however as you can create a custom transparent control to host your image fairly easily. First of all you need to create a new component class.
Once this has been done just modify the code to reflect the following:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace wiindows.utilties // change to your own namespace or even make abstract
{
public partial class trans_comp : Control
{
private readonly Timer update_me;
private Image _image;public trans_comp()
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
update_me = new Timer();
update_me.Tick += TimerOnTick;
update_me.Interval = 200;
update_me.Enabled = true;
update_me.Start();
}protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}protected override void OnMove(EventArgs e)
{
RecreateHandle();
}protected override void OnPaint(PaintEventArgs e)
{
if (_image != null)
{
RectangleF rect = new RectangleF(0, 0, this.Width, this.Height);
e.Graphics.DrawImage(_image, rect);}
}protected override void OnPaintBackground(PaintEventArgs e)
{
//Do not paint background
}public void Redraw()
{
RecreateHandle(); //Hack
}private void TimerOnTick(object source, EventArgs e)
{
RecreateHandle();
update_me.Stop();
}public Image Image
{
get
{
return _image;
}
set
{
_image = value;
RecreateHandle();
}
}
}
}
So there you have it a truly transparent control that can be used to host your image.
say what do you think