【问题标题】:Apply the opacity of a LinearGradientBrush to an image in winforms将LinearGradientBrush的不透明度应用于winforms中的图像
【发布时间】:2017-03-04 06:15:02
【问题描述】:

我想将线性渐变的不透明度应用于图像,但我所能得到的只是在图像顶部绘制的渐变。

this stackoverflow 发布之后,我创建了一个继承自 PictureBox 的用户控件并覆盖了 OnPaint 方法

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        LinearGradientBrush linearGradientBrush = new LinearGradientBrush(
            this.ClientRectangle,
            Color.FromArgb(255, Color.White),
            Color.FromArgb(0, Color.White),
            0f);
        e.Graphics.FillRectangle(linearGradientBrush, this.ClientRectangle);
    }

但是,这只是在图像顶部绘制线性渐变。

如何将线性渐变的不透明度应用到图像?

我在 XAML () 中找到示例,但不适用于 winforms。

【问题讨论】:

    标签: c# winforms opacity lineargradientbrush


    【解决方案1】:

    不必使用用户控件并覆盖OnPaint 事件。只需从空白图像创建图形对象并进行图像处理并将图像分配给PicturePox

    首先将 linearGradientBrush 绘制到背景上,然后在其上绘制图像。始终注意图像操作的顺序。

    FileInfo inputImageFile = new FileInfo(@"C:\Temp\TheSimpsons.png");
    Bitmap inputImage = (Bitmap)Bitmap.FromFile(inputImageFile.FullName);
    
    // create blank bitmap with same size
    Bitmap combinedImage = new Bitmap(inputImage.Width, inputImage.Height);
    
    // create graphics object on new blank bitmap
    Graphics g = Graphics.FromImage(combinedImage);
    
    // also use the same size for the gradient brush and for the FillRectangle function
    LinearGradientBrush linearGradientBrush = new LinearGradientBrush(
        new Rectangle(0,0,combinedImage.Width, combinedImage.Height),
        Color.FromArgb(255, Color.White), //Color.White,
        Color.FromArgb(0, Color.White), // Color.Transparent,
        0f);
    g.FillRectangle(linearGradientBrush, 0, 0, combinedImage.Width, combinedImage.Height);
    
    g.DrawImage(inputImage, 0,0);
    
    previewPictureBox.Image = combinedImage;
    

    结果以黑色作为表单背景颜色,示例图像具有透明度。

    编辑: 我可能误解了意图,并且没有找到像 WPF 那样的简单方法,但这并没有那么困难。

    FileInfo inputImageFile = new FileInfo(@"C:\Temp\TheSimpsons.png");
    Bitmap inputImage = (Bitmap)Bitmap.FromFile(inputImageFile.FullName);
    
    // create blank bitmap
    Bitmap adjImage = new Bitmap(inputImage.Width, inputImage.Height);
    
    // create graphics object on new blank bitmap
    Graphics g = Graphics.FromImage(adjImage);
    
    LinearGradientBrush linearGradientBrush = new LinearGradientBrush(
        new Rectangle(0, 0, adjImage.Width, adjImage.Height),
        Color.White,
        Color.Transparent,
        0f);
    
    Rectangle rect = new Rectangle(0, 0, adjImage.Width, adjImage.Height);
    g.FillRectangle(linearGradientBrush, rect);
    
    int x;
    int y;
    for (x = 0; x < adjImage.Width; ++x)
    {
        for (y = 0; y < adjImage.Height; ++y)
        {
            Color inputPixelColor = inputImage.GetPixel(x, y);
            Color adjPixelColor = adjImage.GetPixel(x, y);
            Color newColor = Color.FromArgb(adjPixelColor.A, inputPixelColor.R, inputPixelColor.G, inputPixelColor.B);
            adjImage.SetPixel(x, y, newColor);
        }
    }
    previewPictureBox.Image = adjImage;
    

    【讨论】:

    • 哇!我想,既然当我第一次问这个问题时没有人回答这个问题,那就永远不会有人回答;我很惊喜你这么久才发现这个问题,并回答它:谢谢!所以,我明白你在说什么:一次做一个像素。好的,我会试试的。谢谢,
    猜你喜欢
    • 2012-03-17
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 2013-06-13
    • 1970-01-01
    相关资源
    最近更新 更多