首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ColorMatrix不适用

ColorMatrix不适用
EN

Stack Overflow用户
提问于 2021-08-23 06:39:14
回答 1查看 61关注 0票数 0

,我想做什么?

以下函数应该简单地返回参数"sourceImage“定义的具有更改后的”不透明度“的图像:

代码语言:javascript
复制
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;
        }

有什么问题?

返回的图像似乎一点也没有改变。

我试过什么?

如果更改以下行

代码语言:javascript
复制
ColorMatrix colorMatrix = new ColorMatrix();

代码语言:javascript
复制
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时遗漏了一些东西。

任何帮助都是感激的,谢谢!

EN

回答 1

Stack Overflow用户

发布于 2021-08-23 06:54:26

好吧,我自己想出来的。

更改这一行:

代码语言:javascript
复制
Image newImage = new Bitmap(sourceImage);

代码语言:javascript
复制
Image newImage = new Bitmap(sourceImage.Width, sourceImage.Height);

是解决办法。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68888240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档