Example #1
0
 public MemImage Clone()
 {
     MemImage clone = new MemImage(Width, Height, this.array);
     return clone;
 }
Example #2
0
        private static void CropSlideImage(string imgPath, Options options)
        {
            Rectangle bounds;
            using (MemoryStream ms = new MemoryStream(System.IO.File.ReadAllBytes(imgPath)))
            {
                var img = new MemImage(System.Drawing.Bitmap.FromStream(ms));

                int left = 0;
                int top = 0;
                int right = img.Width - 1;
                int bottom = img.Height - 1;
                if (options.CropWidth)
                {
                    // scan left side until we encounter a non white pixel
                    for (int i = 0; i < img.Width; i++)
                    {
                        if (!Enumerable.Range(0, img.Height).All(h => { var pd = img.GetPixel(i, h); return pd.R == 255 && pd.G == 255 && pd.B == 255; }))
                        {
                            left = i;
                            break;
                        }
                    }

                    // scan right side until we encounter a non white pixel
                    for (int i = img.Width - 1; i >= 0; i--)
                    {
                        if (!Enumerable.Range(0, img.Height).All(h => { var pd = img.GetPixel(i, h); return pd.R == 255 && pd.G == 255 && pd.B == 255; }))
                        {
                            right = i;
                            break;
                        }
                    }
                    left -= options.CropPadding;
                    right += options.CropPadding;
                    if (left < 0) left = 0;
                    if (right > img.Width - 1) right = img.Width - 1;

                }

                if (options.CropHeight)
                {
                    // scan top side until we encounter a non white pixel
                    for (int i = 0; i < img.Height; i++)
                    {
                        if (!Enumerable.Range(0, img.Width).All(w => { var pd = img.GetPixel(w, i); return pd.R == 255 && pd.G == 255 && pd.B == 255; }))
                        {
                            top = i;
                            break;
                        }
                    }

                    // scan bottom side until we encounter a non white pixel
                    for (int i = img.Height - 1; i >= 0; i--)
                    {
                        if (!Enumerable.Range(0, img.Width).All(w => { var pd = img.GetPixel(w, i); return pd.R == 255 && pd.G == 255 && pd.B == 255; }))
                        {
                            bottom = i;
                            break;
                        }
                    }
                    top -= options.CropPadding;
                    bottom += options.CropPadding;
                    if (top < 0) top = 0;
                    if (bottom > img.Height - 1) bottom = img.Height - 1;
                }
                bounds = Rectangle.FromLTRB(left, top, right, bottom);

                if (bounds.Left == 0 && bounds.Top == 0 && bounds.Width == img.Width - 1 && bounds.Bottom == img.Height - 1)
                {
                    // nothing to crop
                }
                else
                {
                    // crop & overwrite the original image
                    img = MemImage.Crop(img, bounds.X, bounds.Y, bounds.Width, bounds.Height);
                    using (var bmp = img.ToImage())
                        bmp.Save(imgPath, ImageFormat.Png);

                }
            }
        }
Example #3
0
        public static MemImage Crop(MemImage input, int x, int y, int width, int height)
        {
            if (width <= 0 || height <= 0 || x < 0 || y < 0)
                return null;

            int iWidth = input.Width;
            int iHeight = input.Height;

            MemImage output = new MemImage(width, height);

            int jMax = (y + height > iHeight ? iHeight : y + height);
            int iMax = (x + width > iWidth ? iWidth : x + width);

            for (int j = y; j < jMax; j++)
            {
                for (int i = x; i < iMax; i++)
                {
                    PixelData pixelInput = input.GetPixel(i, j);
                    output.SetPixel(i - x, j - y, pixelInput);
                }
            }

            return output;
        }