C# picturebox 加载图片后透明显示在另一控件之上

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static void ChangeOpacity(PictureBox pb, Single opacity, Control parent)
        {
            var img = pb.Image;
            var bmp = new Bitmap(img.Width, img.Height);
            using (var g = Graphics.FromImage(bmp))
            {
                var colorMatrix = new ColorMatrix();
                colorMatrix.Matrix33 = opacity;
                var attr = new ImageAttributes();
                attr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                g.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, attr);
            }
            pb.BackColor = Color.Transparent;
            pb.Image = bmp;
            pb.Parent = parent;
            pb.Left = 0;
            pb.Top = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ChangeOpacity(pictureBox1, 0.3F, button1);
        }
    }
}

这个方法只能保证picturebox仅对其父控件透明
 
原文地址:https://www.cnblogs.com/nanfei/p/10796366.html