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;
return cp;
}
}
protected override void OnMove(EventArgs e)
{
RecreateHandle();
}
private PictureBoxSizeMode _sizeMode = PictureBoxSizeMode.Normal;
public PictureBoxSizeMode SizeMode
{
get { return _sizeMode; }
set
{
_sizeMode = value;
Invalidate();
}
}
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)
{
}
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:
LogoControl.BringToFrontCustom();
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);
}
}