Example #1
0
        //Image resizing
        public static System.Drawing.Image ResizeImage(int maxWidth, int maxHeight, System.Drawing.Image Image)
        {
            int width = Image.Width;
            int height = Image.Height;
            if (width > maxWidth || height > maxHeight)
            {
                //The flips are in here to prevent any embedded image thumbnails -- usually from cameras
                //from displaying as the thumbnail image later, in other words, we want a clean
                //resize, not a grainy one.
                Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX);
                Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX);

                float ratio = 0;
                if (width > height)
                {
                    ratio = (float)width / (float)height;
                    width = maxWidth;
                    height = Convert.ToInt32(Math.Round((float)width / ratio));
                }
                else
                {
                    ratio = (float)height / (float)width;
                    height = maxHeight;
                    width = Convert.ToInt32(Math.Round((float)height / ratio));
                }

                //return the resized image
                return Image.GetThumbnailImage(width, height, null, IntPtr.Zero);
            }

            //return the original resized image
            return Image;
        }
Example #2
0
 private System.Drawing.Image getImage(System.Drawing.Image image, int width, int height)
 {
     if (width == -1) {
         double factor = 1.0 * height / image.Height;
         width = (int)Math.Round(image.Width * factor);
     }
     //
     return image.GetThumbnailImage(width, height, null, IntPtr.Zero);
 }
Example #3
0
        private void DoInitialize(System.Drawing.Bitmap bitmap)
        {
            // get texture's size.
            int targetTextureWidth;
            int targetTextureHeight;
            {
                //	Get the maximum texture size supported by OpenGL.
                int[] textureMaxSize = { 0 };
                GL.GetInteger(GetTarget.MaxTextureSize, textureMaxSize);

                //	Find the target width and height sizes, which is just the highest
                //	posible power of two that'll fit into the image.

                targetTextureWidth = textureMaxSize[0];
                for (int size = 1; size <= textureMaxSize[0]; size *= 2)
                {
                    if (bitmap.Width < size)
                    {
                        targetTextureWidth = size / 2;
                        break;
                    }
                    if (bitmap.Width == size)
                    {
                        targetTextureWidth = size;
                        break;
                    }
                }

                for (int size = 1; size <= textureMaxSize[0]; size *= 2)
                {
                    if (bitmap.Height < size)
                    {
                        targetTextureHeight = size / 2;
                        break;
                    }
                    if (bitmap.Height == size)
                    {
                        targetTextureHeight = size;
                        break;
                    }
                }
            }

            // scale bitmap to right size.
            System.Drawing.Bitmap targetImage = bitmap;
            if (bitmap.Width != targetTextureWidth || bitmap.Height != targetTextureWidth)
            {
                //  Resize the image.
                targetImage = (System.Drawing.Bitmap)bitmap.GetThumbnailImage(targetTextureWidth, targetTextureWidth, null, IntPtr.Zero);
            }

            // generate texture.
            {
                //  Lock the image bits (so that we can pass them to OGL).
                BitmapData bitmapData = targetImage.LockBits(new Rectangle(0, 0, targetImage.Width, targetImage.Height),
                    ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                //GL.ActiveTexture(GL.GL_TEXTURE0);
                GL.GenTextures(1, texture);
                GL.BindTexture(GL.GL_TEXTURE_2D, texture[0]);
                GL.TexImage2D(GL.GL_TEXTURE_2D, 0, (int)GL.GL_RGBA,
                    targetImage.Width, targetImage.Height, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE,
                    bitmapData.Scan0);
                //  Unlock the image.
                targetImage.UnlockBits(bitmapData);
                /* We require 1 byte alignment when uploading texture data */
                //GL.PixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
                /* Clamping to edges is important to prevent artifacts when scaling */
                GL.TexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, (int)GL.GL_CLAMP_TO_EDGE);
                GL.TexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, (int)GL.GL_CLAMP_TO_EDGE);
                /* Linear filtering usually looks best for text */
                GL.TexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, (int)GL.GL_LINEAR);
                GL.TexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, (int)GL.GL_LINEAR);
            }

            // release temp image.
            if (targetImage != bitmap)
            {
                targetImage.Dispose();
            }
        }
Example #4
0
        public static System.Drawing.Image DoEffect(System.Drawing.Image _dImg, eEffect _effect, Primitive parameter)
        {
            if (_effect == eEffect.NONE || null == _dImg) return _dImg;

            System.Drawing.Bitmap bmap = new System.Drawing.Bitmap(_dImg);

            switch (_effect)
            {
                case eEffect.NONE: //None
                    break;
                case eEffect.RED: //Red
                    {
                        System.Drawing.Color c;
                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);
                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, c.R, 0, 0));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.GREEN: //Green
                    {
                        System.Drawing.Color c;
                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);
                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, 0, c.G, 0));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.BLUE: //Blue
                    {
                        System.Drawing.Color c;
                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);
                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, 0, 0, c.B));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.GRAY: //Gray
                    {
                        System.Drawing.Color c;
                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);
                                byte gray = (byte)(.299 * c.R + .587 * c.G + .114 * c.B);
                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, gray, gray, gray));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.INVERSE: //Inverse
                    {
                        FastPixel fp = new FastPixel(bmap);
                        System.Drawing.Color c;
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);
                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, 255 - c.R, 255 - c.G, 255 - c.B));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.YELLOW: //Yellow
                    {
                        System.Drawing.Color c;
                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);
                                byte gray = (byte)(.299 * c.R + .587 * c.G + .114 * c.B);
                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, gray, gray, 0));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.CYAN: //Cyan
                    {
                        System.Drawing.Color c;
                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);
                                byte gray = (byte)(.299 * c.R + .587 * c.G + .114 * c.B);
                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, 0, gray, gray));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.MAGENTA: //Magenta
                    {
                        System.Drawing.Color c;
                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);
                                byte gray = (byte)(.299 * c.R + .587 * c.G + .114 * c.B);
                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, gray, 0, gray));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.SNOW: //Snow
                    {
                        double density = parameter;
                        if (density <= 0) density = pEffect[(int)_effect];
                        Random rand = new Random();
                        System.Drawing.Color c;
                        int i, j;
                        FastPixel fp = new FastPixel(bmap);
                        for (int ii = 0; ii < fp.Width * fp.Height / density; ii++)
                        {
                            i = rand.Next(fp.Width);
                            j = rand.Next(fp.Height);
                            c = fp.GetPixel(i, j);
                            fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, 255, 255, 255));
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.FUZZY: //Fuzzy
                    {
                        Random rand = new Random();
                        System.Drawing.Color c;
                        System.Drawing.Bitmap copy = (System.Drawing.Bitmap)bmap.Clone();
                        FastPixel fpCopy = new FastPixel(copy);
                        int ii, jj;
                        int x = parameter;
                        if (x <= 0) x = pEffect[(int)_effect];
                        int y = 2 * x + 1;
                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                ii = System.Math.Max(0, System.Math.Min(fp.Width - 1, i + rand.Next(y) - x));
                                jj = System.Math.Max(0, System.Math.Min(fp.Height - 1, j + rand.Next(y) - x));
                                c = fpCopy.GetPixel(ii, jj);
                                fp.SetPixel(i, j, c);
                            }
                        }
                        fpCopy.Unlock(false);
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.CONTRAST: //Contrast
                    {
                        double contrast = parameter;
                        if (contrast <= 0) contrast = pEffect[(int)_effect];
                        System.Drawing.Color c;
                        double R, G, B;

                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);

                                R = c.R / 255.0 - 0.5;
                                R = R >= 0 ? System.Math.Pow(R, 1.0 / contrast) : -System.Math.Pow(-R, 1.0 / contrast);
                                R = 255 * (R + 0.5);
                                R = System.Math.Max(0, System.Math.Min(255, R));

                                G = c.G / 255.0 - 0.5;
                                G = G >= 0 ? System.Math.Pow(G, 1.0 / contrast) : -System.Math.Pow(-G, 1.0 / contrast);
                                G = 255 * (G + 0.5);
                                G = System.Math.Max(0, System.Math.Min(255, G));

                                B = c.B / 255.0 - 0.5;
                                B = B >= 0 ? System.Math.Pow(B, 1.0 / contrast) : -System.Math.Pow(-B, 1.0 / contrast);
                                B = 255 * (B + 0.5);
                                B = System.Math.Max(0, System.Math.Min(255, B));

                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, (int)R, (int)G, (int)B));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.BLOCKS: //Blocks
                    {
                        System.Drawing.Image.GetThumbnailImageAbort dummyCallback = new System.Drawing.Image.GetThumbnailImageAbort(ResizeAbort);
                        int size = parameter;
                        if (size <= 0) size = pEffect[(int)_effect];
                        _dImg = _dImg.GetThumbnailImage(bmap.Width / size, bmap.Height / size, dummyCallback, IntPtr.Zero);
                        _dImg = _dImg.GetThumbnailImage(bmap.Width, bmap.Height, dummyCallback, IntPtr.Zero);
                        bmap = new System.Drawing.Bitmap(_dImg);
                    }
                    break;
                case eEffect.REFLECT: //Reflect
                    {
                        int mode = parameter;
                        if (mode <= 0) mode = pEffect[(int)_effect];
                        bmap.RotateFlip(mode == 1 ? System.Drawing.RotateFlipType.RotateNoneFlipY : System.Drawing.RotateFlipType.RotateNoneFlipX);
                    }
                    break;
                case eEffect.JAGGED: //Jagged
                    {
                        System.Drawing.Color c;
                        System.Drawing.Bitmap copy = (System.Drawing.Bitmap)bmap.Clone();
                        FastPixel fpCopy = new FastPixel(copy);
                        int amount = parameter;
                        if (amount <= 0) amount = pEffect[(int)_effect];
                        bool up = true, left = false;
                        int ii, jj;
                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            if (i % amount == 0) up = !up;
                            for (int j = 0; j < fp.Height; j++)
                            {
                                if (j % amount == 0) left = !left;
                                ii = left ? System.Math.Max(0, i - amount) : System.Math.Min(fp.Width - 1, i + amount);
                                jj = up ? System.Math.Max(0, j - amount) : System.Math.Min(fp.Height - 1, j + amount);
                                c = fpCopy.GetPixel(ii, jj);
                                fp.SetPixel(i, j, c);
                            }
                        }
                        fpCopy.Unlock(false);
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.ROTATE: //Rotate
                    {
                        int mode = parameter;
                        if (mode <= 0) mode = pEffect[(int)_effect];
                        bmap.RotateFlip(mode == 1 ? System.Drawing.RotateFlipType.Rotate180FlipNone : mode == 2 ? System.Drawing.RotateFlipType.Rotate270FlipNone : System.Drawing.RotateFlipType.Rotate90FlipNone);
                    }
                    break;
                case eEffect.PIXELATE: //Pixelate
                    {
                        System.Drawing.Color c;
                        System.Drawing.Bitmap copy = (System.Drawing.Bitmap)bmap.Clone();
                        int amount = parameter;
                        if (amount <= 0) amount = pEffect[(int)_effect];
                        FastPixel fpCopy = new FastPixel(copy);
                        FastPixel fp = new FastPixel(bmap);
                        int nx = fp.Width / amount + 1;
                        int ny = fp.Height / amount + 1;
                        int ii, jj;
                        int[,] A = new int[nx, ny];
                        int[,] R = new int[nx, ny];
                        int[,] G = new int[nx, ny];
                        int[,] B = new int[nx, ny];
                        int[,] N = new int[nx, ny];
                        System.Drawing.Color[,] C = new System.Drawing.Color[nx, ny];
                        for (int i = 0; i < nx; i++)
                        {
                            for (int j = 0; j < ny; j++)
                            {
                                A[i, j] = 0;
                                R[i, j] = 0;
                                G[i, j] = 0;
                                B[i, j] = 0;
                                N[i, j] = 0;
                            }
                        }
                        for (int i = 0; i < fp.Width; i++)
                        {
                            ii = i / amount;
                            for (int j = 0; j < fp.Height; j++)
                            {
                                jj = j / amount;
                                c = fpCopy.GetPixel(i, j);
                                A[ii, jj] += c.A;
                                R[ii, jj] += c.R;
                                G[ii, jj] += c.G;
                                B[ii, jj] += c.B;
                                N[ii, jj] += 1;
                                fp.SetPixel(i, j, c);
                            }
                        }
                        for (int i = 0; i < nx; i++)
                        {
                            for (int j = 0; j < ny; j++)
                            {
                                if (N[i,j] > 0) C[i, j] = System.Drawing.Color.FromArgb(A[i, j] / N[i, j], R[i, j] / N[i, j], G[i, j] / N[i, j], B[i, j] / N[i, j]);
                            }
                        }
                        for (int i = 0; i < fp.Width; i++)
                        {
                            ii = i / amount;
                            for (int j = 0; j < fp.Height; j++)
                            {
                                jj = j / amount;
                                fp.SetPixel(i, j, C[ii, jj]);
                            }
                        }
                        fp.Unlock(true);
                        fpCopy.Unlock(false);
                    }
                    break;
                case eEffect.GAMMA: //Gamma
                    {
                        double gamma = parameter;
                        if (gamma <= 0) gamma = pEffect[(int)_effect];
                        System.Drawing.Color c;
                        double R, G, B;

                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);

                                R = 255.0 * System.Math.Pow(c.R / 255.0, gamma);
                                R = System.Math.Max(0, System.Math.Min(255, R));

                                G = 255.0 * System.Math.Pow(c.G / 255.0, gamma);
                                G = System.Math.Max(0, System.Math.Min(255, G));

                                B = 255.0 * System.Math.Pow(c.B / 255.0, gamma);
                                B = System.Math.Max(0, System.Math.Min(255, B));

                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, (int)R, (int)G, (int)B));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.FISHEYE: //FishEye
                    {
                        System.Drawing.Bitmap copy = (System.Drawing.Bitmap)bmap.Clone();
                        double factor = parameter;
                        if (factor < 1) factor = pEffect[(int)_effect];
                        factor -= 1;

                        FastPixel fpCopy = new FastPixel(copy);
                        FastPixel fp = new FastPixel(bmap);
                        double centerX = fp.Width / 2; //center of distortion
                        double centerY = fp.Height / 2;

                        int width = fp.Width; //image bounds
                        int height = fp.Height;

                        xshift = calc_shift(0, centerX - 1, centerX, factor);
                        double newcenterX = width - centerX;
                        double xshift_2 = calc_shift(0, newcenterX - 1, newcenterX, factor);

                        yshift = calc_shift(0, centerY - 1, centerY, factor);
                        double newcenterY = height - centerY;
                        double yshift_2 = calc_shift(0, newcenterY - 1, newcenterY, factor);

                        xscale = (width - xshift - xshift_2) / width;
                        yscale = (height - yshift - yshift_2) / height;

                        for (int j = 0; j < height; j++)
                        {
                            for (int i = 0; i < width; i++)
                            {
                                double x = getRadialX((double)i, (double)j, centerX, centerY, factor);
                                double y = getRadialY((double)i, (double)j, centerX, centerY, factor);
                                int ii = System.Math.Min(width - 1, System.Math.Max(0, (int)x));
                                int jj = System.Math.Min(height - 1, System.Math.Max(0, (int)y));
                                fp.SetPixel(i, j, fpCopy.GetPixel(ii, jj));
                            }
                        }
                        fp.Unlock(true);
                        fpCopy.Unlock(false);
                    }
                    break;
                case eEffect.BULGE: //Bulge
                    {
                        System.Drawing.Bitmap copy = (System.Drawing.Bitmap)bmap.Clone();
                        double factor = parameter;
                        if (factor <= 0) factor = pEffect[(int)_effect];

                        FastPixel fpCopy = new FastPixel(copy);
                        FastPixel fp = new FastPixel(bmap);

                        double rad = System.Math.Min(fp.Width, fp.Height) / 2.0;
                        double dx, dy, dist, scale;
                        int ii, jj;
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                dx = i - fp.Width / 2.0;
                                dy = j - fp.Height / 2.0;
                                dist = System.Math.Sqrt(dx * dx + dy * dy);
                                scale = System.Math.Pow(dist / rad, factor) / (dist / rad);
                                ii = (int)(fp.Width / 2.0 + scale * dx);
                                ii = System.Math.Min(fp.Width - 1, System.Math.Max(0, ii));
                                jj = (int)(fp.Height / 2.0 + scale * dy);
                                jj = System.Math.Min(fp.Height - 1, System.Math.Max(0, jj));
                                fp.SetPixel(i, j, fpCopy.GetPixel(ii, jj));
                            }
                        }
                        fp.Unlock(true);
                        fpCopy.Unlock(false);
                    }
                    break;
                case eEffect.SWIRL: //Swirl
                    {
                        System.Drawing.Bitmap copy = (System.Drawing.Bitmap)bmap.Clone();
                        double factor = parameter;
                        if (factor == 0) factor = pEffect[(int)_effect];

                        FastPixel fpCopy = new FastPixel(copy);
                        FastPixel fp = new FastPixel(bmap);

                        double dx, dy, dist, theta;
                        int ii, jj;
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                dx = i - fp.Width / 2.0;
                                dy = j - fp.Height / 2.0;
                                dist = System.Math.Sqrt(dx * dx + dy * dy);
                                if (dx == 0) theta = dy > 0 ? System.Math.PI / 2.0 : 3.0 * System.Math.PI / 2.0;
                                else theta = System.Math.Atan(dy/dx);
                                if (dx < 0) theta += System.Math.PI;
                                theta += dist / fp.Width * factor * 2 * System.Math.PI;
                                ii = (int)(fp.Width / 2.0 + dist * System.Math.Cos(theta));
                                ii = System.Math.Min(fp.Width - 1, System.Math.Max(0, ii));
                                jj = (int)(fp.Height / 2.0 + dist * System.Math.Sin(theta));
                                jj = System.Math.Min(fp.Height - 1, System.Math.Max(0, jj));
                                fp.SetPixel(i, j, fpCopy.GetPixel(ii, jj));
                            }
                        }
                        fp.Unlock(true);
                        fpCopy.Unlock(false);
                    }
                    break;
                case eEffect.POSTERISE: //Posterise
                    {
                        int level = parameter;
                        if (level <= 0) level = pEffect[(int)_effect];
                        System.Drawing.Color c;
                        double R, G, B;

                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);

                                R = ((int)(c.R / level)) * level;
                                G = ((int)(c.G / level)) * level;
                                B = ((int)(c.B / level)) * level;

                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, (int)R, (int)G, (int)B));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.HUE: //Hue
                    {
                        double hue = parameter;
                        if (hue <= 0) hue = pEffect[(int)_effect];
                        System.Drawing.Color c;
                        double[] HSL;
                        double[] RGB;

                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);

                                HSL = LDColours.RGB2HSL(c.R, c.G, c.B);
                                RGB = LDColours.HSL2RGB(HSL[0] + hue, HSL[1], HSL[2]);

                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, (int)(255*RGB[0]+0.5), (int)(255 * RGB[1] + 0.5), (int)(255 * RGB[2] + 0.5)));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.SATURATION: //Saturation
                    {
                        double saturation = parameter;
                        if (saturation <= 0) saturation = pEffect[(int)_effect];
                        System.Drawing.Color c;
                        double[] HSL;
                        double[] RGB;

                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);

                                HSL = LDColours.RGB2HSL(c.R, c.G, c.B);
                                RGB = LDColours.HSL2RGB(HSL[0], HSL[1] * saturation, HSL[2]);

                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, (int)(255 * RGB[0] + 0.5), (int)(255 * RGB[1] + 0.5), (int)(255 * RGB[2] + 0.5)));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.LIGHTNESS: //Lightness
                    {
                        double lightness = parameter;
                        if (lightness <= 0) lightness = pEffect[(int)_effect];
                        System.Drawing.Color c;
                        double[] HSL;
                        double[] RGB;

                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);

                                HSL = LDColours.RGB2HSL(c.R, c.G, c.B);
                                RGB = LDColours.HSL2RGB(HSL[0], HSL[1], HSL[2] * lightness);

                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, (int)(255 * RGB[0] + 0.5), (int)(255 * RGB[1] + 0.5), (int)(255 * RGB[2] + 0.5)));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                case eEffect.OILPAINT: //Oil Paint
                    {
                        int radius = parameter[1];
                        int levels = parameter[2];
                        if (radius <= 0) radius = pEffect[(int)_effect][1];
                        if (levels <= 0) levels = pEffect[(int)_effect][2];
                        if (radius % 2 == 0) radius++;
                        bmap = fip.OilPaint(bmap, radius, levels);
                    }
                    break;
                case eEffect.CHARCOAL: //Charcoal
                    {
                        bmap = fip.SketchCharcoal(bmap);
                    }
                    break;
                case eEffect.SKETCH: //Pen sketch
                    {
                        bmap = fip.Sketch(bmap);
                    }
                    break;
                case eEffect.CARTOON: //Cartool
                    {
                        int radius = parameter[1];
                        int levels = parameter[2];
                        int inverse = parameter[3];
                        if (radius <= 0) radius = pEffect[(int)_effect][1];
                        if (levels <= 0) levels = pEffect[(int)_effect][2];
                        if (inverse <= 0) inverse = pEffect[(int)_effect][3];
                        if (radius % 2 == 0) radius++;
                        bmap = fip.Cartoon(bmap, radius, levels, inverse);
                        //bmap = fip.Cartoon(bmap, radius, levels, inverse, fip.LaplaceF1());
                    }
                    break;
                case eEffect.EDGE: //Edge
                    {
                        bmap = fip.ImagePrewittFilterColor(bmap);
                        //bmap = fip.ImagePrewittFilterGS(bmap);
                    }
                    break;
                case eEffect.ACCENT: //Accent
                    {
                        int hue = parameter[1];
                        int range = parameter[2];
                        if (hue <= 0) hue = pEffect[(int)_effect][1];
                        if (range <= 0) range = pEffect[(int)_effect][2];
                        bmap = fip.ColorAccent(bmap, hue, range);
                    }
                    break;
                case eEffect.SEPIA: //Sepia
                    {
                        int threshhold = parameter;
                        if (threshhold <= 0) threshhold = pEffect[(int)_effect];
                        bmap = fip.Sepia(bmap, threshhold);
                    }
                    break;
                case eEffect.NOISEREMOVAL: //Noise removal
                    {
                        bmap = fip.ImageSDROMFilterColor(bmap);
                    }
                    break;
                case eEffect.SOLARISE: //Solarise
                    {
                        double power = parameter;
                        if (power <= 0) power = pEffect[(int)_effect];
                        System.Drawing.Color c;
                        double R, G, B;

                        FastPixel fp = new FastPixel(bmap);
                        for (int i = 0; i < fp.Width; i++)
                        {
                            for (int j = 0; j < fp.Height; j++)
                            {
                                c = fp.GetPixel(i, j);

                                R = c.R / 255.0;
                                G = c.G / 255.0;
                                B = c.B / 255.0;

                                R = System.Math.Pow(R < 0.5 ? 1 - 2 * R : 2 * R - 1, power) * 255.0;
                                G = System.Math.Pow(G < 0.5 ? 1 - 2 * G : 2 * G - 1, power) * 255.0;
                                B = System.Math.Pow(B < 0.5 ? 1 - 2 * B : 2 * B - 1, power) * 255.0;

                                fp.SetPixel(i, j, System.Drawing.Color.FromArgb(c.A, (int)R, (int)G, (int)B));
                            }
                        }
                        fp.Unlock(true);
                    }
                    break;
                default:
                    break;
            }

            return (System.Drawing.Image)bmap;
        }
Example #5
0
/*        public void RenderBackgroundList(SpriteBatch sprite, int xShift, int yShift, bool front)
        {
            if (!((ApplicationSettings.visibleTypes & ItemTypes.Backgrounds) == ItemTypes.Backgrounds)) return;
            foreach (BackgroundInstance bg in boardItems.Backgrounds)
                if (bg.front == front && parent.IsItemInRange(bg.X, bg.Y, bg.Width, bg.Height,xShift - bg.Origin.X,yShift - bg.Origin.Y))
                    bg.Draw(sprite, bg.GetColor(ApplicationSettings.editedTypes, selectedLayerIndex, bg.Selected), xShift, yShift);
        }*/

        public System.Drawing.Bitmap ResizeImage(System.Drawing.Bitmap FullsizeImage, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
        {
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            if (OnlyResizeIfWider)
            {
                if (FullsizeImage.Width <= NewWidth)
                {
                    NewWidth = FullsizeImage.Width;
                }
            }

            int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
            if (NewHeight > MaxHeight)
            {
                NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
                NewHeight = MaxHeight;
            }
            System.Drawing.Bitmap NewImage = (System.Drawing.Bitmap)FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
            return NewImage;
        }
 /// <summary>
 /// Bir Image'ın boyutlarını değiştirir.
 /// </summary>
 /// <param name="image"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <returns>Boyutları değiştirilmiş yeni image'ı döndürür.</returns>
 public System.Drawing.Image Resize(System.Drawing.Image image, int width, int height)
 {
     System.Drawing.Image newImage = image.GetThumbnailImage(width, height, null, new IntPtr());
     return newImage;
 }
 private void SaveThumbnail(System.Drawing.Image original, SizeF newSize, string thumbnailFilename)
 {
     using (MemoryStream imageout = new MemoryStream())
     {
         var thumb = original.GetThumbnailImage((int) newSize.Width, (int) newSize.Height, () => false,
             IntPtr.Zero);
         thumb.Save(imageout, ImageFormat.Png);
         _storage.Store(thumbnailFilename, imageout.ToArray(), true);
     }
 }
 private System.Drawing.Image GetReducedImage(System.Drawing.Image image, int width, int height)
 {
     System.Drawing.Image result;
     if (image != null)
     {
         try
         {
             System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(this.ThumbnailCallback);
             System.Drawing.Image reducedImage = image.GetThumbnailImage(width, height, callb, System.IntPtr.Zero);
             image.Dispose();
             result = reducedImage;
             return result;
         }
         catch (System.Exception)
         {
         }
     }
     result = null;
     return result;
 }
Example #9
0
 public static System.Drawing.Bitmap ResizeImage(System.Drawing.Bitmap FullsizeImage, float coeff)
 {
     return (System.Drawing.Bitmap)FullsizeImage.GetThumbnailImage((int)Math.Round(FullsizeImage.Width / coeff), (int)Math.Round(FullsizeImage.Height / coeff), null, IntPtr.Zero);
 }
Example #10
0
        /// <summary>
        /// This method generates the actual thumbnail.
        /// </summary>
        /// <param name="src"></param>
        /// <returns>Thumbnail image</returns>
        private System.Drawing.Image CreateThumbnail(System.Drawing.Image src)
        {
            int maxSize = (int)this._sizeType;

              int w = src.Width;
              int h = src.Height;

              if (w > maxSize)
              {
            h = (h * maxSize) / w;
            w = maxSize;
              }

              if (h > maxSize)
              {
            w = (w * maxSize) / h;
            h = maxSize;
              }

              // The third parameter is required and is of type delegate.  Rather then create a method that
              // does nothing, .NET 2.0 allows for anonymous delegate (similar to anonymous functions in other languages).
              return src.GetThumbnailImage(w, h, delegate() { return false; }, IntPtr.Zero);
        }
Example #11
0
        private byte[] GenerateThumbnail(System.Drawing.Image img, int width, int height)
        {
            var w = img.Width;
            var h = img.Height;

            if (width > 0 && height <= 0) {
                int newHeight = (int)((width * h) / w);
                w = width;
                h = newHeight;
            } else if (height > 0 && width <= 0) {
                int newWidth = (int)((height * w) / h);
                w = newWidth;
                h = height;
            } else {
                w = width;
                h = height;
            }

            using (System.Drawing.Image thumbnailImage = img.GetThumbnailImage(w, h, new System.Drawing.Image.GetThumbnailImageAbort(() => { return true; }), IntPtr.Zero)) {
                using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream()) {
                    thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                    byte[] imageContent = new Byte[imageStream.Length];
                    imageStream.Position = 0;
                    imageStream.Read(imageContent, 0, (int)imageStream.Length);
                    return imageContent;
                }
            }
        }