Transparent PictureBox in C#

In last preject, a transparent picturebox is needed to meet the requiremnt. If you have tried before, you should know that .net transparent is not a real one, the controll only transparent to it's parent, all the controlls betweens the parent and itself will be ignore, it will be very funny if there are some children controll overlap.


No solution is found after search internet, so I decided write my own controll to implement this function.


The main idea is 


1. The controll will override the PictureBox controll。


2. In the OnPaint method, the controll will redraw all the children controlls of it's parent which are below it and have overlap part.


3. In the OnPaint method, the controll will call Invalidate method of all the children controlls of it's parent which are above it.


Following is the code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;


namespace VisualImage
{
    public class TransparentPictureBox:PictureBox
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            ControlCollection cc = this.Parent.Controls;
            if (cc.Count > 1)
            {
                bool isUp = false;
                for (int i = cc.Count - 1; i >= 0; i--)
                {
                    Control c = cc[i];
                    if (isUp)
                    {
                        c.Invalidate();
                    }
                    else
                    {
                        drawRectangleIntersection(g, (TransparentPictureBox)c);
                    }


                    isUp = c == this;
                }
            }


            Bitmap b = new Bitmap(Image);
            g.DrawImage(b, 0, 0, b.Width, b.Height);
            base.OnPaint(e);
        }


        protected void drawRectangleIntersection(Graphics g, TransparentPictureBox tpb)
        {
            Rectangle rectangle1 = new Rectangle(Location.X, Location.Y, Width, Height);
            Rectangle rectangle2 = new Rectangle(tpb.Location.X, tpb.Location.Y, tpb.Width, tpb.Height);


            if (rectangle1.IntersectsWith(rectangle2))
            {
                Rectangle rectangle3 = Rectangle.Intersect(rectangle1, rectangle2);
                if (!rectangle3.IsEmpty)
                {
                    Rectangle srcRect = new Rectangle(rectangle3.Location.X - tpb.Location.X, rectangle3.Location.Y - tpb.Location.Y, rectangle3.Width, rectangle3.Height);
                    Rectangle destRect = new Rectangle(Math.Abs(Location.X - rectangle3.Location.X), Math.Abs(Location.Y - rectangle3.Location.Y), rectangle3.Width, rectangle3.Height);
                    g.DrawImage(tpb.Image, destRect, srcRect, GraphicsUnit.Pixel);
                }
            }
        }
    }
}

download url:  http://download.csdn.net/source/3469956

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值