GDI+:将所有像素设置为给定颜色,同时保留现有的 alpha 值

发布于 2024-08-26 01:53:20 字数 231 浏览 4 评论 0原文

将 System.Drawing.Bitmap 中每个像素的 RGB 分量设置为单一纯色的最佳方法是什么?如果可能的话,我想避免手动循环每个像素来执行此操作。

注意:我想保留原始位图中相同的 alpha 分量。我只想更改 RGB 值。

我研究过使用 ColorMatrixColorMap,但我找不到任何方法可以使用这两种方法将所有像素设置为特定的给定颜色。

What is the best way to set the RGB components of every pixel in a System.Drawing.Bitmap to a single, solid color? If possible, I'd like to avoid manually looping through each pixel to do this.

Note: I want to keep the same alpha component from the original bitmap. I only want to change the RGB values.

I looked into using a ColorMatrix or ColorMap, but I couldn't find any way to set all pixels to a specific given color with either approach.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄暮涼年 2024-09-02 01:53:20

是的,使用 ColorMatrix。它应该看起来像这样:

  0  0  0  0  0
  0  0  0  0  0
  0  0  0  0  0 
  0  0  0  1  0 
  R  G  B  0  1

其中 R、G 和 B 是替换颜色的缩放颜色值(除以 255.0f)

Yes, use a ColorMatrix. It ought to look like this:

  0  0  0  0  0
  0  0  0  0  0
  0  0  0  0  0 
  0  0  0  1  0 
  R  G  B  0  1

Where R, G and B are the scaled color values of the replacement color (divide by 255.0f)

东北女汉子 2024-09-02 01:53:20

我知道这个问题已经得到解答,但根据 Hans Passant 的回答,生成的代码如下所示:

public class Recolor
{
    public static Bitmap Tint(string filePath, Color c)
    {
        // load from file
        Image original = Image.FromFile(filePath);
        original = new Bitmap(original);

        //get a graphics object from the new image
        Graphics g = Graphics.FromImage(original);

        //create the ColorMatrix
        ColorMatrix colorMatrix = new ColorMatrix(
            new float[][]{
                    new float[] {0, 0, 0, 0, 0},
                    new float[] {0, 0, 0, 0, 0},
                    new float[] {0, 0, 0, 0, 0},
                    new float[] {0, 0, 0, 1, 0},
                    new float[] {c.R / 255.0f,
                                 c.G / 255.0f,
                                 c.B / 255.0f,
                                 0, 1}
                });

        //create some image attributes
        ImageAttributes attributes = new ImageAttributes();

        //set the color matrix attribute
        attributes.SetColorMatrix(colorMatrix);

        //draw the original image on the new image
        //using the color matrix
        g.DrawImage(original, 
            new Rectangle(0, 0, original.Width, original.Height),
            0, 0, original.Width, original.Height,
            GraphicsUnit.Pixel, attributes);

        //dispose the Graphics object
        g.Dispose();

        //return a bitmap
        return (Bitmap)original;
    }
}

在此处下载工作演示:http://benpowell.org/change-the-color-of-a-transparent- png-图像-图标-on-the-fly-using-asp-net-mvc/

I know this is already answered, but based on Hans Passant's answer the resulting code looks something like this:

public class Recolor
{
    public static Bitmap Tint(string filePath, Color c)
    {
        // load from file
        Image original = Image.FromFile(filePath);
        original = new Bitmap(original);

        //get a graphics object from the new image
        Graphics g = Graphics.FromImage(original);

        //create the ColorMatrix
        ColorMatrix colorMatrix = new ColorMatrix(
            new float[][]{
                    new float[] {0, 0, 0, 0, 0},
                    new float[] {0, 0, 0, 0, 0},
                    new float[] {0, 0, 0, 0, 0},
                    new float[] {0, 0, 0, 1, 0},
                    new float[] {c.R / 255.0f,
                                 c.G / 255.0f,
                                 c.B / 255.0f,
                                 0, 1}
                });

        //create some image attributes
        ImageAttributes attributes = new ImageAttributes();

        //set the color matrix attribute
        attributes.SetColorMatrix(colorMatrix);

        //draw the original image on the new image
        //using the color matrix
        g.DrawImage(original, 
            new Rectangle(0, 0, original.Width, original.Height),
            0, 0, original.Width, original.Height,
            GraphicsUnit.Pixel, attributes);

        //dispose the Graphics object
        g.Dispose();

        //return a bitmap
        return (Bitmap)original;
    }
}

Download a working demo here: http://benpowell.org/change-the-color-of-a-transparent-png-image-icon-on-the-fly-using-asp-net-mvc/

半衾梦 2024-09-02 01:53:20

最好的(至少就性能而言)选项是使用 Bitmap.LockBits ,并循环扫描线中的像素数据,设置RGB值。

由于您不想更改 Alpha,因此必须循环遍历每个像素 - 没有单个内存分配可以保留 Alpha 并替换 RGB,因为它们是交错在一起的。

The best (in terms of perf, at least) option is to use Bitmap.LockBits, and loop through the pixel data in the scan line, setting the RGB values.

Since you don't want to change the Alpha, you are going to have to loop through each pixel - there is no single memory assignment that will preserve alpha and replace RGB, since they're interleaved together.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文