C#图片特效

public class ImageEffects
    {

        public enum SingleColorTypeEnum { 
            /// <summary>
            /// 平均值法
            /// </summary>
            Average = 1,
            /// <summary>
            /// 最大值法
            /// </summary>
            Max = 2, 
            /// <summary>
            /// 加权平均值法
            /// </summary>
            Weighting = 3 
        };

        /// <summary>
        /// 将图片转换成黑白色效果
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        /// <param name="type">HeiBaiType 枚举</param>
        public static Bitmap SingleColor(Bitmap bmp,SingleColorTypeEnum type)
        {
            Bitmap newBitmap = null;
            try
            {
                int Height = bmp.Height;
                int Width = bmp.Width;
                newBitmap = new Bitmap(Width, Height);
                Color pixel;
                for (int x = 0; x < Width; x++)
                {
                    for (int y = 0; y < Height; y++)
                    {
                        pixel = bmp.GetPixel(x, y);
                        int r, g, b, Result = 0;
                        r = pixel.R;
                        g = pixel.G;
                        b = pixel.B;
                        switch (type)
                        {
                            case SingleColorTypeEnum.Average://平均值法
                                Result = ((r + g + b) / 3);
                                break;
                            case SingleColorTypeEnum.Max://最大值法
                                Result = r > g ? r : g;
                                Result = Result > b ? Result : b;
                                break;
                            case SingleColorTypeEnum.Weighting://加权平均值法
                                Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));
                                break;
                        }
                        newBitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return newBitmap;
        }

        /// <summary>
        /// 将图片转换成黑白色效果
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap SingleColor(Bitmap bmp)
        {
            //确定图像的宽和高
            int width = bmp.Width;
            int height = bmp.Height;

            int pix = 0;
            //LockBits将Bitmap锁定到内存中
            BitmapData data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            unsafe
            {
                //p指向地址
                byte* p = (byte*)data.Scan0;//8位无符号整数
                int offset = data.Stride - width * 3;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        pix = (int)(0.7 * p[0]) + (int)(0.2 * p[1]) + (int)(0.1 * p[2]);
                        pix = Math.Min(255, pix);
                        p[0] = (byte)pix;
                        p[1] = (byte)pix;
                        p[2] = (byte)pix;

                        p += 3;
                    } // x
                    p += offset;
                } // y
            }
            bmp.UnlockBits(data);//从内存中解除锁定

            return bmp;
        }

        /// <summary>
        /// 雾化效果
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap Atomization(Bitmap bmp)
        {
            Bitmap newBitmap = null;
            try
            {
                int Height = bmp.Height;
                int Width = bmp.Width;
                newBitmap = new Bitmap(Width, Height);
                Color pixel;
                for (int x = 1; x < Width - 1; x++)
                {
                    for (int y = 1; y < Height - 1; y++)
                    {
                        Random MyRandom = new Random();
                        int k = MyRandom.Next(123456);
                        //像素块大小
                        int dx = x + k % 19;
                        int dy = y + k % 19;
                        if (dx >= Width)
                            dx = Width - 1;
                        if (dy >= Height)
                            dy = Height - 1;
                        pixel = bmp.GetPixel(dx, dy);
                        newBitmap.SetPixel(x, y, pixel);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return newBitmap;
        }

        /// <summary>
        /// 柔化
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap Soften(Bitmap bmp)
        {
            Bitmap newBitmap = null;
            try
            {
                int Height = bmp.Height;
                int Width = bmp.Width;
                newBitmap = new Bitmap(Width, Height);
                Color pixel;
                //高斯模板
                int[] Gauss ={ 1, 2, 1, 2, 4, 2, 1, 2, 1 };
                for (int x = 1; x < Width - 1; x++)
                    for (int y = 1; y < Height - 1; y++)
                    {
                        int r = 0, g = 0, b = 0;
                        int Index = 0;
                        for (int col = -1; col <= 1; col++)
                            for (int row = -1; row <= 1; row++)
                            {
                                pixel = bmp.GetPixel(x + row, y + col);
                                r += pixel.R * Gauss[Index];
                                g += pixel.G * Gauss[Index];
                                b += pixel.B * Gauss[Index];
                                Index++;
                            }
                        r /= 16;
                        g /= 16;
                        b /= 16;
                        //处理颜色值溢出
                        r = r > 255 ? 255 : r;
                        r = r < 0 ? 0 : r;
                        g = g > 255 ? 255 : g;
                        g = g < 0 ? 0 : g;
                        b = b > 255 ? 255 : b;
                        b = b < 0 ? 0 : b;
                        newBitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                    }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return newBitmap;
        }

        /// <summary>
        /// 锐化效果
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap Sharpen(Bitmap bmp)
        {
            Bitmap newBitmap = null;
            try
            {
                int Height = bmp.Height;
                int Width = bmp.Width;
                newBitmap = new Bitmap(Width, Height);
                Color pixel;
                //拉普拉斯模板
                int[] Laplacian ={ -1, -1, -1, -1, 9, -1, -1, -1, -1 };
                for (int x = 1; x < Width - 1; x++)
                    for (int y = 1; y < Height - 1; y++)
                    {
                        int r = 0, g = 0, b = 0;
                        int Index = 0;
                        for (int col = -1; col <= 1; col++)
                        {
                            for (int row = -1; row <= 1; row++)
                            {
                                pixel = bmp.GetPixel(x + row, y + col); 
                                r += pixel.R * Laplacian[Index];
                                g += pixel.G * Laplacian[Index];
                                b += pixel.B * Laplacian[Index];
                                Index++;
                            }
                        }
                        //处理颜色值溢出
                        r = r > 255 ? 255 : r;
                        r = r < 0 ? 0 : r;

                        g = g > 255 ? 255 : g;
                        g = g < 0 ? 0 : g;

                        b = b > 255 ? 255 : b;
                        b = b < 0 ? 0 : b;

                        newBitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                    }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return newBitmap;
        }

        /// <summary>
        /// 底片效果
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap Negative(Bitmap bmp)
        {
            Bitmap newBitmap = null;
            try
            {
                int Height = bmp.Height;
                int Width = bmp.Width;
                newBitmap = new Bitmap(Width, Height);
                Color pixel;
                for (int x = 1; x < Width; x++)
                {
                    for (int y = 1; y < Height; y++)
                    {
                        int r, g, b;
                        pixel = bmp.GetPixel(x, y);
                        r = 255 - pixel.R;
                        g = 255 - pixel.G;
                        b = 255 - pixel.B;
                        newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return newBitmap;
        }


        /// <summary>
        /// 浮雕效果
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap Relief(Bitmap bmp)
        {
            Bitmap newBitmap = null;
            try
            {
                int Height = bmp.Height;
                int Width = bmp.Width;
                newBitmap = new Bitmap(Width, Height);
                Color pixel1, pixel2;
                for (int x = 0; x < Width - 1; x++)
                {
                    for (int y = 0; y < Height - 1; y++)
                    {
                        int r = 0, g = 0, b = 0;
                        pixel1 = bmp.GetPixel(x, y);
                        pixel2 = bmp.GetPixel(x + 1, y + 1);
                        r = Math.Abs(pixel1.R - pixel2.R + 128);
                        g = Math.Abs(pixel1.G - pixel2.G + 128);
                        b = Math.Abs(pixel1.B - pixel2.B + 128);
                        if (r > 255)
                            r = 255;
                        if (r < 0)
                            r = 0;
                        if (g > 255)
                            g = 255;
                        if (g < 0)
                            g = 0;
                        if (b > 255)
                            b = 255;
                        if (b < 0)
                            b = 0;
                        newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return newBitmap;
        }


        /// <summary>
        /// 日光照射效果
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap Halo(Bitmap bmp)
        {
            Bitmap myImage = null;
            try
            {
                Graphics gh = Graphics.FromImage(bmp);
                int Width = bmp.Width;
                int Height = bmp.Height;
                myImage = bmp.Clone(new RectangleF(0, 0, Width, Height), System.Drawing.Imaging.PixelFormat.DontCare);
                int A = Width / 2;
                int B = Height / 2;
                //MyCenter图片中心点,发亮此值会让强光中心发生偏移
                Point MyCenter = new Point(Width / 2, Height / 2);
                //R强光照射面的半径,即”光晕”
                int R = Math.Min(Width / 2, Height / 2);
                for (int i = Width - 1; i >= 1; i--)
                {
                    for (int j = Height - 1; j >= 1; j--)
                    {
                        float MyLength = (float)Math.Sqrt(Math.Pow((i - MyCenter.X), 2) + Math.Pow((j - MyCenter.Y), 2));
                        //如果像素位于”光晕”之内
                        if (MyLength < R)
                        {
                            Color MyColor = myImage.GetPixel(i, j);
                            int r, g, b;
                            //220亮度增加常量,该值越大,光亮度越强
                            float MyPixel = 220.0f * (1.0f - MyLength / R);
                            r = MyColor.R + (int)MyPixel;
                            r = Math.Max(0, Math.Min(r, 255));
                            g = MyColor.G + (int)MyPixel;
                            g = Math.Max(0, Math.Min(g, 255));
                            b = MyColor.B + (int)MyPixel;
                            b = Math.Max(0, Math.Min(b, 255));
                            //将增亮后的像素值回写到位图
                            Color MyNewColor = Color.FromArgb(255, r, g, b);
                            myImage.SetPixel(i, j, MyNewColor);
                        }
                    }
                    //重新绘制图片
                    gh.Clear(Color.White);
                    gh.DrawImage(myImage, new Rectangle(0, 0, Width, Height));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return myImage;
        }


        /// <summary>
        /// 油画效果
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap Painting(Bitmap bmp)
        {
            Bitmap myImage = null;
            try
            {
                Graphics gh = Graphics.FromImage(bmp);
                int width = bmp.Width;
                int height = bmp.Height;
                RectangleF rect = new RectangleF(0, 0, width, height);
                myImage = bmp.Clone(rect, System.Drawing.Imaging.PixelFormat.DontCare);
                //产生随机数序列
                Random rnd = new Random();
                //取不同的值决定油画效果的不同程度
                int iModel = 2;
                int i = width - iModel;
                while (i > 1)
                {
                    int j = height - iModel;
                    while (j > 1)
                    {
                        int iPos = rnd.Next(100000) % iModel;
                        //将该点的RGB值设置成附近iModel点之内的任一点
                        Color color = myImage.GetPixel(i + iPos, j + iPos);
                        myImage.SetPixel(i, j, color);
                        j = j - 1;
                    }
                    i = i - 1;
                }
                //重新绘制图像
                gh.Clear(Color.White);
                gh.DrawImage(myImage, new Rectangle(0, 0, width, height));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return myImage;
        }


        /// <summary>
        /// 积木特效
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap Blocks(Bitmap bmp)
        {
            Bitmap bitmap = null;
            try
            {
                Graphics myGraphics = Graphics.FromImage(bmp) ;
                int myWidth, myHeight, i, j, iAvg, iPixel;
                Color myColor, myNewColor;
                RectangleF myRect;
                myWidth = bmp.Width;
                myHeight = bmp.Height;
                myRect = new RectangleF(0, 0, myWidth, myHeight);
                bitmap = bmp.Clone(myRect, System.Drawing.Imaging.PixelFormat.DontCare);
                i = 0;
                while (i < myWidth - 1)
                {
                    j = 0;
                    while (j < myHeight - 1)
                    {
                        myColor = bitmap.GetPixel(i, j);
                        iAvg = (myColor.R + myColor.G + myColor.B) / 3;
                        iPixel = 0;
                        if (iAvg >= 128)
                            iPixel = 255;
                        else
                            iPixel = 0;
                        myNewColor = Color.FromArgb(255, iPixel, iPixel, iPixel);
                        bitmap.SetPixel(i, j, myNewColor);
                        j = j + 1;
                    }
                    i = i + 1;
                }
                myGraphics.Clear(Color.WhiteSmoke);
                myGraphics.DrawImage(bitmap, new Rectangle(0, 0, myWidth, myHeight));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return bitmap;
        }

    }
public class ImageEffectsFast
    {

        public enum SingleColorTypeEnum
        {
            /// <summary>
            /// 平均值法
            /// </summary>
            Average = 1,
            /// <summary>
            /// 最大值法
            /// </summary>
            Max = 2,
            /// <summary>
            /// 加权平均值法
            /// </summary>
            Weighting = 3
        };

        /// <summary>
        /// 将图片转换成黑白色效果
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        /// <param name="type">HeiBaiType 枚举</param>
        public static Bitmap SingleColor(Bitmap bmp)
        {
            return SingleColor(bmp, SingleColorTypeEnum.Weighting);
        }

        /// <summary>
        /// 将图片转换成黑白色效果
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap SingleColor(Bitmap bmp, SingleColorTypeEnum type)
        {
            //确定图像的宽和高
            int width = bmp.Width;
            int height = bmp.Height;

            int pix = 0;
            //LockBits将Bitmap锁定到内存中
            BitmapData data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            unsafe
            {
                //p指向地址
                byte* p = (byte*)data.Scan0;//8位无符号整数
                int offset = data.Stride - width * 3;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        switch (type)
                        {
                            case SingleColorTypeEnum.Average://平均值法
                                pix = (p[0] + p[1] + p[2]) / 3;
                                break;
                            case SingleColorTypeEnum.Max://最大值法
                                pix = p[0] > p[1] ? p[0] : p[1];
                                pix = pix > p[2] ? pix : p[2];
                                break;
                            case SingleColorTypeEnum.Weighting://加权平均值法
                                pix = (int)(0.7 * p[0]) + (int)(0.2 * p[1]) + (int)(0.1 * p[2]);
                                break;
                        }
                        if (pix < 0) pix = 0;
                        if(pix>0) pix = Math.Min(255, pix);
                        p[0] = (byte)pix;
                        p[1] = (byte)pix;
                        p[2] = (byte)pix;

                        p += 3;
                    } // x
                    p += offset;
                } // y
            }
            bmp.UnlockBits(data);//从内存中解除锁定

            return bmp;
        }

        /// <summary>
        /// 底片效果
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap Negative(Bitmap bmp)
        {
            //确定图像的宽和高
            int width = bmp.Width;
            int height = bmp.Height;

            //LockBits将Bitmap锁定到内存中
            BitmapData data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            unsafe
            {
                //p指向地址
                byte* p = (byte*)data.Scan0;//8位无符号整数
                int offset = data.Stride - width * 3;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            p[i] = (byte)(255 - p[i]);
                        }
                        p += 3;
                    } // x
                    p += offset;
                } // y
            }
            bmp.UnlockBits(data);//从内存中解除锁定

            return bmp;
        }


        /// <summary>
        /// 浮雕效果
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap Relief(Bitmap bmp)
        {
            //确定图像的宽和高
            int width = bmp.Width;
            int height = bmp.Height;

            //LockBits将Bitmap锁定到内存中
            BitmapData data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            unsafe
            {
                //p指向地址
                byte* p = (byte*)data.Scan0;//8位无符号整数
                int offset = data.Stride - width * 3;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        int r = 0, g = 0, b = 0;
                        if (x < width - 1&&y<height-1)
                        {
                            r = Math.Abs(p[0] - p[3 * (width + 1)] + 128);
                            g = Math.Abs(p[1] - p[3 * (width + 1) + 1] + 128);
                            b = Math.Abs(p[2] - p[3 * (width + 1) + 2] + 128);
                        }
                        else
                        {
                            r = 128;
                            g = 128;
                            b = 128;
                        }

                        if (r > 255) r = 255;
                        if (r < 0) r = 0;
                        if (g > 255) g = 255;
                        if (g < 0) g = 0;
                        if (b > 255) b = 255;
                        if (b < 0) b = 0;
                        p[0] = (byte)r;
                        p[1] = (byte)g;
                        p[2] = (byte)b;
                        p += 3;
                    } // x
                    p += offset;
                } // y
            }
            bmp.UnlockBits(data);//从内存中解除锁定
            return bmp;
        }


        /// <summary>
        /// 积木特效
        /// </summary>
        /// <param name="bmp">Bitmap 对象</param>
        public static Bitmap Blocks(Bitmap bmp)
        {
            //确定图像的宽和高
            int width = bmp.Width;
            int height = bmp.Height;

            //LockBits将Bitmap锁定到内存中
            BitmapData data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            unsafe
            {
                //p指向地址
                byte* p = (byte*)data.Scan0;//8位无符号整数
                int offset = data.Stride - width * 3;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        byte b = (byte)((p[0] + p[1] + p[2]) / 3);
                        if (b > 128) b = 255; else b = 0;
                        p[0] = b;
                        p[1] = b;
                        p[2] = b;
                        p += 3;
                    } // x
                    p += offset;
                } // y
            }
            bmp.UnlockBits(data);//从内存中解除锁定

            return bmp;
        }


        /// <summary>
        /// 亮度调节
        /// </summary>
        /// <param name="b">Bitmap 对象</param>
        /// <param name="degree">亮度 Min=-255  Max=255</param>
        public static Bitmap KiLighten(Bitmap b, int degree)
        {
            if (b == null)
            {
                return null;
            }
            //确定最小值和最大值
            if (degree < -255) degree = -255;
            if (degree > 255) degree = 255;
            try
            {
                //确定图像的宽和高
                int width = b.Width;
                int height = b.Height;

                int pix = 0;
                //LockBits将Bitmap锁定到内存中
                BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                unsafe
                {
                    //p指向地址
                    byte* p = (byte*)data.Scan0;//8位无符号整数
                    int offset = data.Stride - width * 3;
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            // 处理指定位置像素的亮度
                            for (int i = 0; i < 3; i++)
                            {
                                pix = p[i] + degree;
                                if (degree < 0) p[i] = (byte)Math.Max(0, pix);
                                if (degree > 0) p[i] = (byte)Math.Min(255, pix);
                            } // i
                            p += 3;
                        } // x
                        p += offset;
                    } // y
                }
                b.UnlockBits(data);//从内存中解除锁定

                return b;
            }
            catch
            {
                return null;
            }
        }


        /// <summary>
        /// 对比度调节
        /// </summary>
        /// <param name="b">Bitmap 对象</param>
        /// <param name="degree">对比度 Min=-100 Max=100</param>
        public static Bitmap KiContrast(Bitmap b, int degree)
        {
            if (b == null)
            {
                return null;
            }

            if (degree < -100) degree = -100;
            if (degree > 100) degree = 100;

            try
            {

                double pixel = 0;
                double contrast = (100.0 + degree) / 100.0;
                contrast *= contrast;
                int width = b.Width;
                int height = b.Height;
                BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                unsafe
                {
                    byte* p = (byte*)data.Scan0;
                    int offset = data.Stride - width * 3;
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            // 处理指定位置像素的对比度
                            for (int i = 0; i < 3; i++)
                            {
                                pixel = ((p[i] / 255.0 - 0.5) * contrast + 0.5) * 255;
                                if (pixel < 0) pixel = 0;
                                if (pixel > 255) pixel = 255;
                                p[i] = (byte)pixel;
                            } // i
                            p += 3;
                        } // x
                        p += offset;
                    } // y
                }
                b.UnlockBits(data);
                return b;
            }
            catch
            {
                return null;
            }
        }
    }

 

posted @ 2012-07-31 09:12  stone87654321  阅读(1026)  评论(0编辑  收藏  举报