【发布时间】: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