Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Drawing.Imaging;
- using System.Drawing;
- namespace WpfApplication2
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- var new_image = AutoCrop(new Bitmap(@"C:\Users\STUDENT\Desktop\old.jpg"));
- new_image.Save(@"C:\Users\STUDENT\Desktop\new.jpg", ImageFormat.Jpeg);
- }
- private void Display(BitmapSource bmpSource)
- {
- }
- private void ApplyDisplayChanges()
- {
- }
- // Returns a byte array containing the rgb values of each pixel
- public byte[][] GetRGB(Bitmap bmp)
- {
- BitmapData bmp_data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
- IntPtr ptr = bmp_data.Scan0;
- int num_pixels = bmp.Width * bmp.Height, num_bytes = bmp_data.Stride * bmp.Height, padding = bmp_data.Stride - bmp.Width * 3, i = 0, ct = 1;
- byte[] r = new byte[num_pixels], g = new byte[num_pixels], b = new byte[num_pixels], rgb = new byte[num_bytes];
- System.Runtime.InteropServices.Marshal.Copy(ptr, rgb, 0, num_bytes);
- for (int x = 0; x < num_bytes - 3; x += 3)
- {
- if (x == (bmp_data.Stride * ct - padding)) { x += padding; ct++; };
- r[i] = rgb[x]; g[i] = rgb[x + 1]; b[i] = rgb[x + 2]; i++;
- }
- bmp.UnlockBits(bmp_data);
- return new byte[3][] { r, g, b };
- }
- public System.Drawing.Image AutoCrop(Bitmap bmp)
- {
- //Get an array containing the R,G,B components of each pixel
- var pixels = GetRGB(bmp);
- int h = bmp.Height - 1;
- int w = bmp.Width;
- int top = 0;
- int bottom = h;
- int left = bmp.Width;
- int right = 0;
- int white = 0;
- int tolerance = 95; // 95%
- bool prev_color = false;
- for (int i = 0; i < pixels[0].Length; i++)
- {
- int x = (i % (w));
- int y = (int)(Math.Floor((decimal)(i / w)));
- int tol = 255 * tolerance / 100;
- if (pixels[0][i] >= tol && pixels[1][i] >= tol && pixels[2][i] >= tol)
- {
- white++;
- right = (x > right && white == 1) ? x : right;
- }
- else
- {
- left = (x < left && white >= 1) ? x : left;
- right = (x == w - 1 && white == 0) ? w - 1 : right;
- white = 0;
- }
- if (white == w)
- {
- top = (y - top < 3) ? y : top;
- bottom = (prev_color && x == w - 1 && y > top + 1) ? y : bottom;
- }
- left = (x == 0 && white == 0) ? 0 : left;
- bottom = (y == h && x == w - 1 && white != w && prev_color) ? h + 1 : bottom;
- if (x == w - 1)
- {
- prev_color = (white < w) ? true : false;
- white = 0;
- }
- }
- right = (right == 0) ? w : right;
- left = (left == w) ? 0 : left;
- //Crop the image
- Bitmap bmpCrop = bmp.Clone(new System.Drawing.Rectangle(left, top, right - left + 1, bottom - top), bmp.PixelFormat);
- return bmpCrop as Bitmap;
- }
- }
- }
Add Comment
Please, Sign In to add comment