转:设置图片透明的四种方法

参照博文:http://blog.csdn.net/lexiaoyao20/article/details/6628830

最有效的一种方法:

矩阵法:

这个方法的效率最高。

 

    /// <summary>  
           /// 设置图片的透明度  
           /// </summary>  
           /// <param name="image">原图</param>  
           /// <param name="alpha">透明度0-255</param>  
           /// <returns></returns>  
           private Bitmap SetPictureAlpha(Image image,int alpha)  
           {  
               //颜色矩阵  
               float[][] matrixItems =  
               {  
                   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,alpha/255f,0},  
                   new float[]{0,0,0,0,1}  
               };  
               ColorMatrix colorMatrix = new ColorMatrix(matrixItems);  
               ImageAttributes imageAtt = new ImageAttributes();  
               imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);  
               Bitmap bmp = new Bitmap(image.Width, image.Height);  
               Graphics g = Graphics.FromImage(bmp);  
               g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),  
                       0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAtt);  
               g.Dispose();  
      
               return bmp;  
           }  

 

posted @ 2016-07-21 19:18  寒夜美美  阅读(729)  评论(0编辑  收藏  举报