Ejemplo n.º 1
0
        /// <summary>
        /// 看起來像底片負片的效果
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static 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;

            // Assumes alpha is in the rightmost byte, change as needed

            for (int x = 1; x < width; x++)
            {
                for (int y = 1; y < height; y++)
                {
                    int r, g, b;
                    pixel = lbmp.GetPixel(x, y);
                    //newlbmp.SetPixel(x, y, Color.FromArgb((int) (0xFFFFFF00u ^ pixel.ToArgb())));
                    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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 浮雕
        /// 找附近像赤 r1, abs(r-r2+128)
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static 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);
        }
Ejemplo n.º 3
0
        //柔化
        public static 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);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 灰階
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Bitmap GrayImage(Bitmap bmp, GrayAlgorithmTypes 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 GrayAlgorithmTypes.Average:     //平均值
                        Result = ((r + g + b) / 3);
                        break;

                    case GrayAlgorithmTypes.Maximum:     //最大值
                        Result = r > g ? r : g;
                        Result = Result > b ? Result : b;
                        break;

                    case GrayAlgorithmTypes.WeightedAverage:     //加權平均值
                        Result = ((int)(0.299 * r) + (int)(0.587 * g) + (int)(0.114 * b));
                        break;
                    }
                    newlbmp.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
                }
            }
            lbmp.UnlockBits();
            newlbmp.UnlockBits();
            return(newbmp);
        }
Ejemplo n.º 5
0
        //雾化
        public static 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);
        }