c#改变图片透明度
·
全栈工程师开发手册 (作者:栾鹏)
c#教程全解
c#改变图片透明度,需要保存成png格式,透明度参数alpha的取值范围为0-255
测试函数
static void Main()
{
Bitmap b = file2img("test.jpg");
Bitmap bb = img_alpha(b,200);
img2file(bb, "test1.png");
}
改变透明度的函数
public static Bitmap img_alpha(Bitmap src, int alpha)
{
Bitmap bmp = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for (int h = 0; h < src.Height; h++)
for (int w = 0; w < src.Width; w++)
{
Color c = src.GetPixel(w, h);
bmp.SetPixel(w, h, Color.FromArgb(alpha, c.R, c.G, c.B));//色彩度最大为255,最小为0
}
return bmp;
}
图片读取和存储函数
//图片读取
public static Bitmap file2img(string filepath)
{
Bitmap b = new Bitmap(filepath);
return b;
}
//图片生成
public static void img2file(Bitmap b, string filepath)
{
b.Save(filepath);
}
更多推荐

所有评论(0)