/// <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 方法。
https://www.cnblogs.com/whisht/archive/2013/05/02/3085060.html