Winforms C#渐变不透明度位图

Winforms C#渐变不透明度位图,c#,.net,winforms,controls,opacity,C#,.net,Winforms,Controls,Opacity,我想制作一个应用了线性不透明度的位图。(也就是说,它在左侧更加不透明,在接近右侧时逐渐变得不那么不透明。) 这可能吗?我知道可以有一个恒定的不透明度级别。将位图放入PictureBox控件中。然后,您必须使用PictureBox的Paint事件处理程序来进行淡入淡出。差不多 private void pictureBox_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawImage(pictureBox.Image, 0, 0

我想制作一个应用了线性不透明度的位图。(也就是说,它在左侧更加不透明,在接近右侧时逐渐变得不那么不透明。)


这可能吗?我知道可以有一个恒定的不透明度级别。

将位图放入
PictureBox
控件中。然后,您必须使用
PictureBox
Paint
事件处理程序来进行淡入淡出。差不多

private void pictureBox_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(pictureBox.Image, 0, 0, 
        pictureBox.ClientRectangle, GraphicsUnit.Pixel);
    Color left = Color.FromArgb(128, Color.Blue);
    Color right = Color.FromArgb(128, Color.Red);
    LinearGradientMode direction = LinearGradientMode.Horizontal;
    LinearGradientBrush brush = new LinearGradientBrush(
        pictureBox.ClientRectangle, left, right, direction);
    e.Graphics.FillRectangle(brush, pictureBox.ClientRectangle);
}
此示例将图像覆盖从蓝色淡入红色。我相信你可以玩这个来得到你想要的工作

我希望这有帮助

我知道它可能有一个恒定的不透明度水平

所以不要将其设置为常量,LinearGradientBrush在插值alpha值时没有问题。具有背景图像集的表单的简单演示:

    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);
        var rc = new Rectangle(20, 20, this.ClientSize.Width - 40, 50);
        using (var brush = new System.Drawing.Drawing2D.LinearGradientBrush(
            rc, 
            Color.FromArgb(255, Color.BlueViolet), 
            Color.FromArgb(0,   Color.BlueViolet), 
            0f)) {
                e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
                e.Graphics.FillRectangle(brush, rc);
        }
    }
制作: