C#中图片透明

/// <summary> /// 处理图片透明操作 /// </summary> /// <param name="srcImage">原始图片</param> /// <param name="opacity">透明度(0.0---1.0)</param> /// <returns></returns> private Image TransparentImage(Image srcImage, float opacity) {     float[][] nArray ={ new float[] {1, 0, 0, 0, 0},                         new float[] {0, 1, 0, 0, 0},                         new float[] {0, 0, 1, 0, 0},                         new float[] {0, 0, 0, opacity, 0},                         new float[] {0, 0, 0, 0, 1}};     ColorMatrix matrix = new ColorMatrix(nArray);     ImageAttributes attributes = new ImageAttributes();     attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);     Bitmap resultImage = new Bitmap(srcImage.Width, srcImage.Height);     Graphics g = Graphics.FromImage(resultImage);     g.DrawImage(srcImage, new Rectangle(0, 0, srcImage.Width, srcImage.Height), 0, 0, srcImage.Width, srcImage.Height, GraphicsUnit.Pixel, attributes);      return resultImage; }

ColorMatrix:定义包含 RGBA 空间坐标的 5 x 5 矩阵。ImageAttributes 类的若干方法通过使用颜色矩阵调整图像颜色。


ImageAttributes​ 对象包含有关在呈现时如何操作位图和图元文件颜色的信息。ImageAttributes 对象维护多个颜色调整设置,包括颜色调整矩阵、灰度调整矩阵、灰度校正值、颜色映射表和颜色阈值。呈现过程中,可以对颜色进行校正、调暗、调亮和移除。要应用这些操作,应初始化一个 ImageAttributes 对象,并将该 ImageAttributes 对象的路径(连同 Image​ 的路径)传递给 DrawImage 方法。