您的位置:首页 > 编程语言 > C#

c#改变图片透明度

2017-10-04 08:53 274 查看
全栈工程师开发手册 (作者:栾鹏)

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: