C# 将LinearGradientBrush的不透明度应用于winforms中的图像

C# 将LinearGradientBrush的不透明度应用于winforms中的图像,c#,winforms,opacity,lineargradientbrush,C#,Winforms,Opacity,Lineargradientbrush,我想对图像应用线性渐变的不透明度,但我能得到的只是在图像顶部绘制的渐变 在本文之后,我创建了一个继承自PictureBox的用户控件,并重写了OnPaint方法 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); LinearGradientBrush linearGradientBrush = new LinearGradientBrush(

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

在本文之后,我创建了一个继承自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。

无需使用用户控件并重写
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;
FileInfo-inputImageFile=newfileinfo(@“C:\Temp\TheSimpsons.png”);
位图inputImage=(位图)Bitmap.FromFile(inputImageFile.FullName);
//创建空白位图
位图调整图像=新位图(inputImage.Width、inputImage.Height);
//在新的空白位图上创建图形对象
Graphics g=Graphics.FromImage(adjImage);
LinearGradientBrush LinearGradientBrush=新的LinearGradientBrush(
新矩形(0,0,adjImage.Width,adjImage.Height),
颜色,白色,
颜色,透明,
0f);
矩形rect=新矩形(0,0,adjImage.Width,adjImage.Height);
g、 FillRectangle(linearGradientBrush,rect);
int x;
int-y;
对于(x=0;x

哇!我想,既然我第一次问这个问题时没有人回答,就没有人会回答;我很惊喜你这么久之后才发现这个问题,并回答:谢谢!我明白你的意思了:一次做一个像素。好的,我试试看。谢谢