【问题标题】:ColorMatrix does not apply颜色矩阵不适用
【发布时间】:2021-10-23 13:07:12
【问题描述】:

我想做什么?

下面的函数应该简单地返回由参数“sourceImage”定义的图像,改变了“不透明度”:

public static Image DisableImage(Image sourceImage)
        {
            Image newImage = new Bitmap(sourceImage);

            using (Graphics g = Graphics.FromImage(newImage))
            {
                using (ImageAttributes imageAttributes = new ImageAttributes())
                {
                    ColorMatrix colorMatrix = new ColorMatrix();
                    colorMatrix.Matrix33 = 0.5f;
                    imageAttributes.SetColorMatrix(colorMatrix);

                    //Draw the original image on the new image using the color matrix
                    g.DrawImage(sourceImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel, imageAttributes);
                }
            }

            return newImage;
        }

有什么问题?

返回的图像似乎根本没有改变。

我尝试了什么?

如果更改以下行

ColorMatrix colorMatrix = new 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[] {Color.Red.R / 255.0f,
                             Color.Red.G / 255.0f,
                             Color.Red.B / 255.0f,
                             0, 1}
                });

在图像上绘制了一个红色的半透明“胶片”,这让我怀疑我错过了如何使用 ColorMatrix。

感谢任何帮助,谢谢!

【问题讨论】:

标签: c# image colormatrix


【解决方案1】:

好吧,我自己想通了。

改变这一行:

Image newImage = new Bitmap(sourceImage);

Image newImage = new Bitmap(sourceImage.Width, sourceImage.Height);

是解决方案。

我可能是在现有图像之上绘制的,这当然不是我想要的。 有没有可能删除这个问题?

【讨论】:

    猜你喜欢
    • 2010-10-21
    • 1970-01-01
    • 2013-08-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多