Guest User

Untitled

a guest
Jul 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Drawing.Imaging;
  15. using System.Drawing;
  16.  
  17. namespace WpfApplication2
  18. {
  19.     /// <summary>
  20.     /// Interaction logic for MainWindow.xaml
  21.     /// </summary>
  22.     public partial class MainWindow : Window
  23.     {
  24.         public MainWindow()
  25.         {
  26.             InitializeComponent();
  27.  
  28.             var new_image = AutoCrop(new Bitmap(@"C:\Users\STUDENT\Desktop\old.jpg"));
  29.             new_image.Save(@"C:\Users\STUDENT\Desktop\new.jpg", ImageFormat.Jpeg);
  30.         }
  31.  
  32.         private void Display(BitmapSource bmpSource)
  33.         {
  34.  
  35.         }
  36.  
  37.         private void ApplyDisplayChanges()
  38.         {
  39.  
  40.         }
  41.  
  42.  
  43.         // Returns a byte array containing the rgb values of each pixel
  44.         public byte[][] GetRGB(Bitmap bmp)
  45.         {
  46.             BitmapData bmp_data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  47.             IntPtr ptr = bmp_data.Scan0;
  48.             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;
  49.             byte[] r = new byte[num_pixels], g = new byte[num_pixels], b = new byte[num_pixels], rgb = new byte[num_bytes];
  50.             System.Runtime.InteropServices.Marshal.Copy(ptr, rgb, 0, num_bytes);
  51.  
  52.             for (int x = 0; x < num_bytes - 3; x += 3)
  53.             {
  54.                 if (x == (bmp_data.Stride * ct - padding)) { x += padding; ct++; };
  55.                 r[i] = rgb[x]; g[i] = rgb[x + 1]; b[i] = rgb[x + 2]; i++;
  56.             }
  57.             bmp.UnlockBits(bmp_data);
  58.             return new byte[3][] { r, g, b };
  59.         }
  60.  
  61.         public System.Drawing.Image AutoCrop(Bitmap bmp)
  62.         {
  63.             //Get an array containing the R,G,B components of each pixel
  64.             var pixels = GetRGB(bmp);
  65.  
  66.             int h = bmp.Height - 1;
  67.             int w = bmp.Width;
  68.            
  69.             int top    = 0;
  70.             int bottom = h;
  71.             int left   = bmp.Width;
  72.             int right  = 0;
  73.            
  74.             int white = 0;
  75.             int tolerance = 95; // 95%
  76.  
  77.             bool prev_color = false;
  78.             for (int i = 0; i < pixels[0].Length; i++)
  79.             {
  80.                 int x = (i % (w));
  81.                 int y = (int)(Math.Floor((decimal)(i / w)));
  82.                 int tol = 255 * tolerance / 100;
  83.  
  84.                 if (pixels[0][i] >= tol && pixels[1][i] >= tol && pixels[2][i] >= tol)
  85.                 {
  86.                     white++;
  87.                     right = (x > right && white == 1) ? x : right;
  88.                 }
  89.                 else
  90.                 {
  91.                     left = (x < left && white >= 1) ? x : left;
  92.                     right = (x == w - 1 && white == 0) ? w - 1 : right;
  93.                     white = 0;
  94.                 }
  95.  
  96.                 if (white == w)
  97.                 {
  98.                     top = (y - top < 3) ? y : top;
  99.                     bottom = (prev_color && x == w - 1 && y > top + 1) ? y : bottom;
  100.                 }
  101.  
  102.                 left = (x == 0 && white == 0) ? 0 : left;
  103.                 bottom = (y == h && x == w - 1 && white != w && prev_color) ? h + 1 : bottom;
  104.  
  105.                 if (x == w - 1)
  106.                 {
  107.                     prev_color = (white < w) ? true : false;
  108.                     white = 0;
  109.                 }
  110.             }
  111.             right = (right == 0) ? w : right;
  112.             left = (left == w) ? 0 : left;
  113.  
  114.             //Crop the image
  115.             Bitmap bmpCrop = bmp.Clone(new System.Drawing.Rectangle(left, top, right - left + 1, bottom - top), bmp.PixelFormat);
  116.  
  117.             return bmpCrop as Bitmap;
  118.         }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.     }
  125. }
Add Comment
Please, Sign In to add comment