在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
逐个像素进行Alpha值的设置,网上其他的代码不能处理有透明背景的图片,因此要对Alpha、R、G、B均为0的透明色进行特殊处理,不做转换。
1 private Bitmap SetImageOpacity(Image srcImage, int opacity) 2 { 3 Bitmap pic = new Bitmap(srcImage); 4 for (int w = 0; w < pic.Width; w++) 5 { 6 for (int h = 0; h < pic.Height; h++) 7 { 8 Color c = pic.GetPixel(w, h); 9 Color newC; 10 if (!c.Equals(Color.FromArgb(0, 0, 0, 0))) 11 { 12 newC = Color.FromArgb(opacity, c); 13 } 14 else 15 { 16 newC = c; 17 } 18 pic.SetPixel(w, h, newC); 19 } 20 } 21 return pic; 22 } 23 24 private Image SetImageOpacity2(Image srcImage, int opacity) 25 { 26 Bitmap img = new Bitmap(srcImage); 27 using (Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) 28 { 29 using (Graphics g = Graphics.FromImage(bmp)) 30 { 31 g.DrawImage(img, 0, 0); 32 for (int h = 0; h <= img.Height - 1; h++) 33 { 34 for (int w = 0; w <= img.Width - 1; w++) 35 { 36 Color c = img.GetPixel(w, h); 37 if (!c.Equals(Color.FromArgb(0, 0, 0, 0))) 38 { 39 bmp.SetPixel(w, h, Color.FromArgb(opacity, c.R, c.G, c.B)); 40 } 41 else 42 { 43 bmp.SetPixel(w, h, Color.FromArgb(c.A, c.R, c.G, c.B)); 44 } 45 } 46 } 47 } 48 return (Image)bmp.Clone(); 49 } 50 }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论