设置图片透明度的四种方法 (转)

http://blog.csdn.net/lexiaoyao20/article/details/6628830

 

方法一:像素法

即循环扫描整张图片,设置每一个像素的Alpha通道的值。 由于是一个个像素的处理,所以这种方法效率很低。

  1. /// <summary>   
  2.         /// 设置图片的透明度   
  3.         /// </summary>   
  4.         /// <param name="bitmap">原图</param>   
  5.         /// <param name="alpha">透明度(0-255之间)</param>   
  6.         /// <returns></returns>   
  7.         public static Bitmap SetBitmapAlpha(Bitmap bitmap, int alpha)  
  8.         {  
  9.             Bitmap bmp = (Bitmap)bitmap.Clone();  
  10.             Color color;  
  11.             for (int x = 0; x < bmp.Width; x++)  
  12.             {  
  13.                 for (int y = 0; y < bmp.Height; y++)  
  14.                 {  
  15.                     color = bmp.GetPixel(x, y);  
  16.                     bmp.SetPixel(x, y, Color.FromArgb(alpha, color));  
  17.                 }  
  18.             }  
  19.   
  20.             bitmap.Dispose();  
  21.             return bmp;  
  22.         }  


方法二:内存法

  1. public static Bitmap SetBitmapAlpha(Bitmap bitmap, int alpha)  
  2.         {  
  3.             Bitmap bmp = (Bitmap)bitmap.Clone();  
  4.   
  5.             Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);  
  6.             System.Drawing.Imaging.BitmapData bmpData =  
  7.                 bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,  
  8.                 PixelFormat.Format32bppArgb);  
  9.   
  10.             IntPtr ptr = bmpData.Scan0;  //获取首地址   
  11.             int bytes = Math.Abs(bmpData.Stride) * bmp.Height;  
  12.             byte[] argbValues = new byte[bytes];  
  13.   
  14.             //复制ARGB的值到数组   
  15.             System.Runtime.InteropServices.Marshal.Copy(ptr, argbValues, 0, bytes);  
  16.   
  17.             for (int count = 3; count < argbValues.Length; count += 4)  
  18.             {  
  19.                 //设置alpha通道的值   
  20.                 argbValues[count] = (byte)alpha;  
  21.             }  
  22.   
  23.             System.Runtime.InteropServices.Marshal.Copy(argbValues, 0, ptr, bytes);  
  24.             bmp.UnlockBits(bmpData);  
  25.   
  26.   
  27.             bitmap.Dispose();  
  28.             return bmp;  
  29.         }  


方法三:指针法

  1. public static Bitmap SetBitmapAlpha(Bitmap bitmap, int alpha)  
  2.         {  
  3.             Bitmap bmp = (Bitmap)bitmap.Clone();  
  4.   
  5.             Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);  
  6.             BitmapData bmpData = bmp.LockBits(rect,  
  7.                 ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);  
  8.   
  9.             unsafe  
  10.             {  
  11.                 byte* p = (byte*)bmpData.Scan0;  
  12.                 int offset = bmpData.Stride - bmp.Width * 4;  
  13.                 for (int y = 0; y < bmp.Height; y++)  
  14.                 {  
  15.                     for (int x = 0; x < bmp.Width; x++)  
  16.                     {  
  17.                         //设置alpha通道的值   
  18.                         p[3] = (byte)alpha;  
  19.                         p += 4;  
  20.                     }  
  21.                     p += offset;  
  22.                 }  
  23.             }  
  24.   
  25.             bmp.UnlockBits(bmpData);  
  26.             bitmap.Dispose();  
  27.   
  28.             return bmp;  
  29.         }  


 

方法四:矩阵法

这个方法的效率最高。

  1. /// <summary>   
  2.        /// 设置图片的透明度   
  3.        /// </summary>   
  4.        /// <param name="image">原图</param>   
  5.        /// <param name="alpha">透明度0-255</param>   
  6.        /// <returns></returns>   
  7.        private Bitmap SetPictureAlpha(Image image,int alpha)  
  8.        {  
  9.            //颜色矩阵   
  10.            float[][] matrixItems =  
  11.            {  
  12.                new float[]{1,0,0,0,0},  
  13.                new float[]{0,1,0,0,0},  
  14.                new float[]{0,0,1,0,0},  
  15.                new float[]{0,0,0,alpha/255f,0},  
  16.                new float[]{0,0,0,0,1}  
  17.            };  
  18.            ColorMatrix colorMatrix = new ColorMatrix(matrixItems);  
  19.            ImageAttributes imageAtt = new ImageAttributes();  
  20.            imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);  
  21.            Bitmap bmp = new Bitmap(image.Width, image.Height);  
  22.            Graphics g = Graphics.FromImage(bmp);  
  23.            g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),  
  24.                    0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAtt);  
  25.            g.Dispose();  
  26.   
  27.            return bmp;  
  28.        }  
posted @ 2012-03-02 23:56  董雨  阅读(1356)  评论(0编辑  收藏  举报