zoukankan      html  css  js  c++  java
  • 【C#】图片处理(底片,黑白,锐化,柔化,浮雕,雾化)

    通过GDI+对图片数据进行处理,下面列出各个效果的算法

    对于读取图片的每个像素颜色的速度非常慢,这里使用LockBitmap类来对图片像素进行操作,LockBitmap类的定义看这里

    http://www.cnblogs.com/bomo/archive/2013/02/26/2934055.html

     1、旋转(90度,180度,270度)

            //旋转90,180,270
            public Bitmap RotateImage(Bitmap bmp, int angle)
            {
                if (angle != 90 && angle != 180 && angle != 270)
                {
                    return null;
                }
                int width = bmp.Width;
                int height = bmp.Height;
    
                if (angle == 90)
                {
                    Bitmap newbmp = new Bitmap(height, width);
                    using (Graphics g = Graphics.FromImage(newbmp))
                    {
                        Point[] destinationPoints = {
                            new Point(height, 0), // destination for upper-left point of original
                            new Point(height, width),// destination for upper-right point of original
                            new Point(0, 0)}; // destination for lower-left point of original
                        g.DrawImage(bmp, destinationPoints);
                    }
                    return newbmp;
                }
    
                if (angle == 180)
                {
                    Bitmap newbmp = new Bitmap(width, height);
                    using (Graphics g = Graphics.FromImage(newbmp))
                    {
                        Point[] destinationPoints = {
                            new Point(width, height), // destination for upper-left point of original
                            new Point(0, height),// destination for upper-right point of original
                            new Point(width, 0)}; // destination for lower-left point of original
                        g.DrawImage(bmp, destinationPoints);
                    }
                    return newbmp;
                }
    
                if(angle == 270)
                {
                    Bitmap newbmp = new Bitmap(height, width);
                    using (Graphics g = Graphics.FromImage(newbmp))
                    {
                        Point[] destinationPoints = {
                            new Point(0, width), // destination for upper-left point of original
                            new Point(0, 0),// destination for upper-right point of original
                            new Point(height, width)}; // destination for lower-left point of original
                        g.DrawImage(bmp, destinationPoints);
                    }
                    return newbmp;
                }
                return null;
            }

    2、重设大小

            //重设大小
            public Bitmap ResizeImage(Bitmap bmp, Size size)
            {
                Bitmap newbmp = new Bitmap(size.Width, size.Height);
                using (Graphics g = Graphics.FromImage(newbmp))
                {
                    g.DrawImage(bmp, new Rectangle(Point.Empty, size));
                }
                return newbmp;
            }

    3、底片效果(反色)(255-r, 255-g, 255-b)

            //底片
            public Bitmap NegativeImage(Bitmap bmp)
            {
                int height = bmp.Height;
                int width = bmp.Width;
                Bitmap newbmp = new Bitmap(width, height);
    
                LockBitmap lbmp = new LockBitmap(bmp);
                LockBitmap newlbmp = new LockBitmap(newbmp);
                lbmp.LockBits();
                newlbmp.LockBits();
    
                Color pixel;
                for (int x = 1; x < width; x++)
                {
                    for (int y = 1; y < height; y++)
                    {
                        int r, g, b;
                        pixel = lbmp.GetPixel(x, y);
                        r = 255 - pixel.R;
                        g = 255 - pixel.G;
                        b = 255 - pixel.B;
                        newlbmp.SetPixel(x, y, Color.FromArgb(r, g, b));
                    }
                }
                lbmp.UnlockBits();
                newlbmp.UnlockBits();
                return newbmp;
            }

    4、黑白效果

            //黑白
            public Bitmap GrayImage(Bitmap bmp, int type)
            {
                int height = bmp.Height;
                int width = bmp.Width;
                Bitmap newbmp = new Bitmap(width, height);
    
                LockBitmap lbmp = new LockBitmap(bmp);
                LockBitmap newlbmp = new LockBitmap(newbmp);
                lbmp.LockBits();
                newlbmp.LockBits();
    
                Color pixel;
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        pixel = lbmp.GetPixel(x, y);
                        int r, g, b, Result = 0;
                        r = pixel.R;
                        g = pixel.G;
                        b = pixel.B;
                        switch (type)
                        {
                            case 0://平均值法
                                Result = ((r + g + b) / 3);
                                break;
                            case 1://最大值法
                                Result = r > g ? r : g;
                                Result = Result > b ? Result : b;
                                break;
                            case 2://加权平均值法
                                Result = ((int)(0.3 * r) + (int)(0.59 * g) + (int)(0.11 * b));
                                break;
                        }
                        newlbmp.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
                    }
                }
                lbmp.UnlockBits();
                newlbmp.UnlockBits();
                return newbmp;
            }

    5、浮雕:找出附近的像素点r1,abs(r-r2+128)

            //浮雕
            public Bitmap EmbossmentImage(Bitmap bmp)
            {
                int height = bmp.Height;
                int width = bmp.Width;
                Bitmap newbmp = new Bitmap(width, height);
    
                LockBitmap lbmp = new LockBitmap(bmp);
                LockBitmap newlbmp = new LockBitmap(newbmp);
                lbmp.LockBits();
                newlbmp.LockBits();
    
                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 = lbmp.GetPixel(x, y);
                        pixel2 = lbmp.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;
                        newlbmp.SetPixel(x, y, Color.FromArgb(r, g, b));
                    }
                }
                lbmp.UnlockBits();
                newlbmp.UnlockBits();
                return newbmp;
            }

    6、柔化

            //柔化
            public Bitmap SoftenImage(Bitmap bmp)
            {
                int height = bmp.Height;
                int width = bmp.Width;
                Bitmap newbmp = new Bitmap(width, height);
    
                LockBitmap lbmp = new LockBitmap(bmp);
                LockBitmap newlbmp = new LockBitmap(newbmp);
                lbmp.LockBits();
                newlbmp.LockBits();
    
                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 = lbmp.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;
                        newlbmp.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                    }
                }
                lbmp.UnlockBits();
                newlbmp.UnlockBits();
                return newbmp;
            }

     

    7、锐化

            //锐化
            public Bitmap SharpenImage(Bitmap bmp)
            {
                int height = bmp.Height;
                int width = bmp.Width;
                Bitmap newbmp = new Bitmap(width, height);
    
                LockBitmap lbmp = new LockBitmap(bmp);
                LockBitmap newlbmp = new LockBitmap(newbmp);
                lbmp.LockBits();
                newlbmp.LockBits();
    
                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 = lbmp.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;
                        newlbmp.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                    }
                }
                lbmp.UnlockBits();
                newlbmp.UnlockBits();
                return newbmp;
            }

    8、雾化

            //雾化
            public Bitmap AtomizationImage(Bitmap bmp)
            {
                int height = bmp.Height;
                int width = bmp.Width;
                Bitmap newbmp = new Bitmap(width, height);
    
                LockBitmap lbmp = new LockBitmap(bmp);
                LockBitmap newlbmp = new LockBitmap(newbmp);
                lbmp.LockBits();
                newlbmp.LockBits();
    
                System.Random MyRandom = new Random();
                Color pixel;
                for (int x = 1; x < width - 1; x++)
                {
                    for (int y = 1; y < height - 1; y++)
                    {
                        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 = lbmp.GetPixel(dx, dy);
                        newlbmp.SetPixel(x, y, pixel);
                    }
                }
                lbmp.UnlockBits();
                newlbmp.UnlockBits();
                return newbmp;
            }

     

  • 相关阅读:
    DOS命令收集
    iis6配置支持.net4.0
    正则表达式限制文本框
    剖析XML(第一讲)
    DataTime.ToString("xx") 转换
    .net面试题大汇集
    django学习笔记(一)
    django学习笔记(一)
    django学习笔记(二)
    django学习笔记(二)
  • 原文地址:https://www.cnblogs.com/bomo/p/2939453.html
Copyright © 2011-2022 走看看