简体   繁体   English

Winforms C#渐变不透明度位图

[英]Winforms C# Gradient Opacity bitmap

I want to make a bitmap that has a linear opacity applied. 我想制作一个具有线性不透明度的位图。 (ie it is more opaque on the left side and get progressively less opaque as it approaches the right.) (即,它在左侧更不透明,并且在接近右侧时逐渐变得不透明。)

Is this possible? 这可能吗? I know its possible to have a constant opacity level. 我知道它可能具有恒定的不透明度。

I know its possible to have a constant opacity level 我知道它可能具有恒定的不透明度

So don't make it constant, LinearGradientBrush has no trouble interpolating the alpha value. 因此,请不要使其恒定,LinearGradientBrush插值alpha值没有问题。 A simple demonstration of a form that has the BackgroundImage set: 具有BackgroundImage设置的表单的简单演示:

    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);
        }
    }

Produced: 制作:

在此处输入图片说明

Put the BitMap into a PictureBox control. 将BitMap放入PictureBox控件中。 Then you will have to use the PictureBox 's Paint event handler to do the fading. 然后,您将不得不使用PictureBoxPaint事件处理程序进行淡入淡出。 Something like 就像是

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);
}

This example fades the image overlay from blue to red. 本示例将图像叠加层从蓝色渐变为红色。 I am sure you can play with this to get what you want working. 我确信您可以使用它来获得您想要的工作。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM