• 简单图片处理


        public static class ImageProcess
        {
            /// <summary>
            /// 图片旋转
            /// </summary>
            /// <param name="image"></param>
            /// <param name="angle">旋转角度</param>
            /// <returns></returns>
            public static Image Rotation(Image image,float angle)
            {
    
                angle = 360 - angle % 360;            //弧度转换
                double radian = angle * Math.PI / 180.0;
                double cos = Math.Cos(radian);
                double sin = Math.Sin(radian);
                //原图的宽和高
                int w = image.Width;
                int h = image.Height;
                int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
                int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
                //目标位图
                Bitmap dsImage = new Bitmap(W, H);
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //计算偏移量
                Point Offset = new Point((W - w) / 2, (H - h) / 2);
                //构造图像显示区域:让图像的中心与窗口的中心点一致
                Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
                Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
                g.TranslateTransform(center.X, center.Y);
                g.RotateTransform(360 - angle);
                //恢复图像在水平和垂直方向的平移
                g.TranslateTransform(-center.X, -center.Y);
                g.DrawImage(image, rect);
                //重至绘图的所有变换
                g.ResetTransform();
                g.Save();
                g.Dispose();
                return dsImage;
            }
    
            /// <summary>
            /// 图片翻转
            /// </summary>
            /// <param name="image"></param>
            /// <param name="type">1水平,2垂直,3水平垂直</param>
            /// <returns></returns>
            public static Image Turn(Image image,int type)
            {
                if (type == 1)
                {
                    image.RotateFlip(RotateFlipType.RotateNoneFlipX);
                }
                else if (type == 2)
                {
                    image.RotateFlip(RotateFlipType.RotateNoneFlipY);
                }
                else if (type == 3)
                {
                    image.RotateFlip(RotateFlipType.RotateNoneFlipXY);
                }
                return image;
            }
    
            /// <summary>
            /// 底片效果
            /// </summary>
            /// <param name="image"></param>
            /// <returns></returns>
            public static Image Film(Image image)
            {
                try
                {
                    int height = image.Height;
                    int width = image.Width;
                    Bitmap newbitmap = new Bitmap(width, height);
                    Bitmap oldbitmap = (Bitmap)image;
                    Color pixel;
                    for (int x = 1; x < width; x++)
                    {
                        for (int y = 1; y < height; y++)
                        {
                            int r, g, b;
                            pixel = oldbitmap.GetPixel(x, y);
                            r = 255 - pixel.R;
                            g = 255 - pixel.G;
                            b = 255 - pixel.B;
                            newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                        }
                    }
                    return newbitmap;
                }
                catch
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 浮雕效果
            /// </summary>
            /// <param name="image"></param>
            /// <returns></returns>
            public static Image Relief(Image image)
            {
                try
                {
                    int height = image.Height;
                    int width = image.Width;
                    Bitmap newbitmap = new Bitmap(width, height);
                    Bitmap oldbitmap = (Bitmap)image;
                    Color pixel1, pixel2;
                    for (int x = 0; x < width - 1; x++)
                    {
                        for (int y = 0; y < height - 1; y++)
                        {
                            int r, g, b;
                            pixel1 = oldbitmap.GetPixel(x, y);
                            pixel2 = oldbitmap.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));
                        }
                    }
                    return newbitmap;
                }
                catch
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 黑白效果
            /// </summary>
            /// <param name="image"></param>
            /// <returns></returns>
            public static Image BlackAndWhite(Image image)
            {
                try
                {
                    int height = image.Height;
                    int width = image.Width;
                    Bitmap newbitmap = new Bitmap(width, height);
                    Bitmap oldbitmap = (Bitmap)image;
                    Color pixel;
                    for (int x = 0; x < width; x++)
                        for (int y = 0; y < height; y++)
                        {
                            pixel = oldbitmap.GetPixel(x, y);
                            int r, g, b, Result = 0;
                            r = pixel.R;
                            g = pixel.G;
                            b = pixel.B;
                            //实例程序以加权平均值法产生黑白图像
                            int iType = 2;
                            switch (iType)
                            {
                                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.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));
                                    break;
                            }
                            newbitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
                        }
                    return newbitmap;
                }
                catch
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 柔化效果
            /// </summary>
            /// <param name="image"></param>
            /// <returns></returns>
            public static Image Soften(Image image)
            {
                try
                {
                    int height = image.Height;
                    int width = image.Width;
                    Bitmap newbitmap = new Bitmap(width, height);
                    Bitmap oldbitmap = (Bitmap)image;
                    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 = oldbitmap.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));
                        }
                    }
                    return newbitmap;
                }
                catch
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 锐化效果
            /// </summary>
            /// <param name="image"></param>
            /// <returns></returns>
            public static Image Sharpen(Image image)
            {
                try
                {
                    int height = image.Height;
                    int width = image.Width;
                    Bitmap newbitmap = new Bitmap(width, height);
                    Bitmap oldbitmap = (Bitmap)image;
                    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 = oldbitmap.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));
                        }
                    }
                    return newbitmap;
                }
                catch
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 雾化效果
            /// </summary>
            /// <param name="image"></param>
            /// <returns></returns>
            public static Image Atomization(Image image)
            {
                try
                {
                    int height = image.Height;
                    int width = image.Width;
                    Bitmap newbitmap = new Bitmap(width, height);
                    Bitmap oldbitmap = (Bitmap)image;
                    Color pixel;
                    for (int x = 1; x < width - 1; x++)
                    {
                        for (int y = 1; y < height - 1; y++)
                        {
                            System.Random MyRandom = new Random();
                            int k = MyRandom.Next(123456);
                            //像素块大小
                            int dx = x + k % 9;
                            int dy = y + k % 9;
                            if (dx >= width)
                                dx = width - 1;
                            if (dy >= height)
                                dy = height - 1;
                            pixel = oldbitmap.GetPixel(dx, dy);
                            newbitmap.SetPixel(x, y, pixel);
                        }
                    }
                    return newbitmap;
                }
                catch
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 图片明暗调整
            /// </summary>
            /// <param name="image"></param>
            /// <param name="degree">亮度[-255,255]</param>
            /// <returns></returns>
            public static Image Brightness(Image image, int degree)
            {
                try
                {
                    Bitmap b = (Bitmap)image;
                    if (degree < -255) degree = -255;
                    if (degree > 255) degree = 255;
                    int width = image.Width;
                    int height = image.Height;
                    int pix = 0;
    
                    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++)
                                {
                                    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 image;
                }
                catch
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 灰色效果
            /// </summary>
            /// <param name="image"></param>
            /// <returns></returns>
            public static Image Gray(Image image)
            {
                try
                {
                    Bitmap currentBitmap = (Bitmap)image.Clone();
                    Graphics g = Graphics.FromImage(currentBitmap);
                    ImageAttributes ia = new ImageAttributes();
                    float[][] colorMatrix =   {    
                    new   float[]   {0.299f,   0.299f,   0.299f,   0,   0},
                    new   float[]   {0.587f,   0.587f,   0.587f,   0,   0},
                    new   float[]   {0.114f,   0.114f,   0.114f,   0,   0},
                    new   float[]   {0,   0,   0,   1,   0},
                    new   float[]   {0,   0,   0,   0,   1}
                                              };
                    ColorMatrix cm = new ColorMatrix(colorMatrix);
                    ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                    g.DrawImage(currentBitmap, new Rectangle(0, 0, currentBitmap.Width, currentBitmap.Height), 0, 0, currentBitmap.Width, currentBitmap.Height, GraphicsUnit.Pixel, ia);
                    g.Dispose();
                    return (Image)(currentBitmap.Clone());
                }
                catch
                {
                    return null;
                }
                
            }
    
            /// <summary>
            /// 图像对比度调整
            /// </summary>
            /// <param name="b">原始图</param>
            /// <param name="degree">对比度[-100, 100]</param>
            /// <returns></returns>
            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;
                }
            } 
        }
    

      

  • 相关阅读:
    GZOI 2017配对统计 树状数组
    关于线段树的一些问题
    BZOJ 压力 tarjan 点双联通分量+树上差分+圆方树
    洛谷4552 差分
    洛谷5026 Lycanthropy 差分套差分
    【锁】MySQL和Oracle行锁比较
    oracle体系结构
    【加密】RSA验签及加密
    【Shiro】SpringBoot集成Shiro
    【Eureka】实现原理
  • 原文地址:https://www.cnblogs.com/fuchao2014/p/3781204.html
Copyright © 2020-2023  润新知