图片对比度亮度调节函数

//调节bitmap的亮度函数
        public bool Brightness(Bitmap bmpSender,int intBright)
        {
            try
            {
                if ( intBright < -255 || intBright > 255 )
                {
                    return false;
                }
                BitmapData bdataSender = bmpSender.LockBits
                    (
                    new Rectangle(0,0,bmpSender.Width,bmpSender.Height),
                    ImageLockMode.ReadWrite,
                    bmpSender.PixelFormat
                    );
           
                int intStride = bdataSender.Stride;
                IntPtr iptrScan0 = bdataSender.Scan0;

                int intValue = 0;

                unsafe
                {
                    byte* bptr = (byte*)(void*)iptrScan0;
                    int intWidth = bmpSender.Width * 3;
                    int intOffset = intStride - intWidth;
                    for ( int y = 0; y < bmpSender.Height; ++y )
                    {
                        for ( int x = 0; x < intWidth; ++x )
                        {

                            intValue = (int) (bptr[0] + intBright);
                            if (intValue < 0)
                            {
                                intValue = 0;
                            }
                            else if (intValue > 255)
                            {
                                intValue = 255;
                            }
                            bptr[0] = (byte)intValue;
                            ++bptr;
                        }
                        bptr += intOffset;
                    }
                }
                bmpSender.UnlockBits(bdataSender);
            }
            catch
            {
           
            }
            return true;
           
        }


        //调整Bitmap对比度函数
        public Bitmap Contrast(Bitmap bmpSender,float fltValue)
        {
            try
            {
                if ( fltValue < 0 || fltValue > 2 )
                {
                    return null;
                }

                int intB = Convert.ToInt32( -127 * fltValue + 127 );

                BitmapData bdataSender = bmpSender.LockBits
                    (
                    new Rectangle(0, 0, bmpSender.Width,bmpSender.Height),
                    ImageLockMode.ReadWrite,
                    bmpSender.PixelFormat
                    );

                int intStride = bdataSender.Stride;
                System.IntPtr iptrScan0 = bdataSender.Scan0;

                unsafe
                {
                    byte* bptr = (byte*)(void*)iptrScan0;
                    int intOffset = intStride - bmpSender.Width * 3;
                    int intWidth = bmpSender.Width * 3;
                    for(int y = 0;y < bmpSender.Height; ++y)
                    {
                        for(int x = 0; x < intWidth; ++x )
                        {
                            int k = (int)(bptr[0] * fltValue + intB);
                            if( k > 255 )
                            {
                                k = 255;
                            }
                            else if( k < 0 )
                            {
                                k = 0;
                            }
                            bptr[0] = (byte)k;
                            ++bptr;
                        }
                        bptr += intOffset;
                    }
                }
                bmpSender.UnlockBits(bdataSender);
            }
            catch
            {
           
            }
            return bmpSender;
           
        }
      
原文地址:https://www.cnblogs.com/jcomet/p/1242819.html