我一直在使用我从here找到的代码来更改图像的亮度
public Bitmap SetBrightness(Bitmap bmap, int brightness)
{
if (brightness < -255) brightness = -255;
if (brightness > 255) brightness = 255;
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
int cR = c.R + brightness;
int cG = c.G + brightness;
int cB = c.B + brightness;
if (cR < 0) cR = 1;
if (cR > 255) cR = 255;
if (cG < 0) cG = 1;
if (cG > 255) cG = 255;
if (cB < 0) cB = 1;
if (cB > 255) cB = 255;
bmap.SetPixel(i, j, Color.FromArgb((byte)cR, (byte)cG, (byte)cB));
}
}
return bmap;
}
它适用于非透明图像,但是当尝试将其应用于透明图像时,它也会填充透明部分。例如,here是我原始图像之前的透明度和here之后的透明度。我只想让图像的不透明部分变暗,而不是透明的背景。
答案 0 :(得分:1)
当您致电Color.FromArgb
时,您未提供Alpha频道,请尝试此操作,看看它是否有效:
public Bitmap SetBrightness(Bitmap bmap, int brightness)
{
if (brightness < -255) brightness = -255;
if (brightness > 255) brightness = 255;
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
int cR = c.R + brightness;
int cG = c.G + brightness;
int cB = c.B + brightness;
if (cR < 0) cR = 1;
if (cR > 255) cR = 255;
if (cG < 0) cG = 1;
if (cG > 255) cG = 255;
if (cB < 0) cB = 1;
if (cB > 255) cB = 255;
bmap.SetPixel(i, j, Color.FromArgb(c.A, (byte)cR, (byte)cG, (byte)cB));
}
}
return bmap;
}