You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中修改图像颜色?Windows应用黑色背景适配需求

当然可行!这在Windows应用开发里是个很常见的需求,我给你两种适配不同框架的实现思路,你可以根据自己的项目类型来选:

方案1:适用于WinForms/传统GDI+桌面应用

如果你的项目是基于WinForms或者用GDI+来处理图像,最高效的方式是用**颜色矩阵(ColorMatrix)**来实现颜色反转(黑色转白色本质就是RGB通道的反转),比逐个遍历像素要快很多。

下面是C#的示例代码:

using System.Drawing;
using System.Drawing.Imaging;

public Bitmap ConvertBlackToWhite(Bitmap originalImage)
{
    // 创建一个和原图大小一致的新位图
    Bitmap convertedImage = new Bitmap(originalImage.Width, originalImage.Height);
    
    // 创建颜色矩阵:反转RGB通道,保留Alpha通道
    ColorMatrix colorMatrix = new ColorMatrix(new float[][]
    {
        new float[] {-1, 0, 0, 0, 0},   // Red通道反转
        new float[] {0, -1, 0, 0, 0},   // Green通道反转
        new float[] {0, 0, -1, 0, 0},   // Blue通道反转
        new float[] {0, 0, 0, 1, 0},    // Alpha通道保留
        new float[] {1, 1, 1, 0, 1}     // 补偿偏移,让黑色(-1*0+1=1)变成白色
    });
    
    // 设置图像属性
    ImageAttributes attributes = new ImageAttributes();
    attributes.SetColorMatrix(colorMatrix);
    
    // 用新的颜色属性绘制图像
    using (Graphics g = Graphics.FromImage(convertedImage))
    {
        g.DrawImage(originalImage, new Rectangle(0, 0, originalImage.Width, originalImage.Height),
                    0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel, attributes);
    }
    
    return convertedImage;
}

调用的时候,直接把下载后的Bitmap传入这个方法,再把返回的结果绑定到控件上就行。

方案2:适用于WPF应用

WPF里的图像处理更偏向于声明式或者用Shader效果,这里给你两种简单的方式:

方式A:用ColorTransform(简单快速)

如果你的图像是纯黑(没有灰度渐变),可以直接在XAML里给Image加一个ColorTransform:

<Image Source="你的下载图像路径">
    <Image.RenderTransform>
        <ColorTransform R="1" G="1" B="1" A="1" OffsetR="1" OffsetG="1" OffsetB="1"/>
        <!-- 公式:新颜色 = 原始颜色 * R + OffsetR,黑色(0,0,0)就会变成(1,1,1)即白色 -->
    </Image.RenderTransform>
</Image>

方式B:用PixelShader(支持复杂灰度转换)

如果图像有灰度阴影,想要把所有深色都转成对应的浅色,可以用自定义Shader:

  1. 创建一个.hlsl文件,内容如下:
sampler2D input : register(s0);

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 color = tex2D(input, uv);
    // 反转RGB通道,保留Alpha
    color.rgb = 1 - color.rgb;
    return color;
}
  1. 编译成.ps文件后,在WPF里引用:
public class InvertShaderEffect : ShaderEffect
{
    public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(InvertShaderEffect), 0);
    public static readonly DependencyProperty InvertAmountProperty = DependencyProperty.Register("InvertAmount", typeof(double), typeof(InvertShaderEffect), new UIPropertyMetadata(1.0, PixelShaderConstantCallback(0)));

    public InvertShaderEffect()
    {
        PixelShader pixelShader = new PixelShader();
        pixelShader.UriSource = new Uri("pack://application:,,,/你的项目名;component/InvertShader.ps");
        this.PixelShader = pixelShader;

        this.UpdateShaderValue(InputProperty);
        this.UpdateShaderValue(InvertAmountProperty);
    }

    public Brush Input
    {
        get { return (Brush)GetValue(InputProperty); }
        set { SetValue(InputProperty, value); }
    }

    public double InvertAmount
    {
        get { return (double)GetValue(InvertAmountProperty); }
        set { SetValue(InvertAmountProperty, value); }
    }
}

然后在XAML里使用:

<Image Source="你的下载图像路径">
    <Image.Effect>
        <local:InvertShaderEffect/>
    </Image.Effect>
</Image>
额外注意事项
  • 如果你的图像带有Alpha透明通道,上面的方案都会保留透明效果,不用担心背景透出来的问题。
  • 处理大图像时,建议放在异步线程里执行(比如用Task.Run),避免阻塞UI线程导致应用卡顿。
  • 如果图像不是纯黑,而是有其他深色,你可以调整颜色矩阵的参数或者Shader里的逻辑,设置一个阈值(比如只把RGB值低于0.2的像素转成白色)。

内容的提问来源于stack exchange,提问作者Rachel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:56:32