using Microsoft.Win32; using System; using System.IO; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Media.Imaging; namespace Clip { class Clip { [STAThread] static void Main(string[] args) { var openFileDialog = new OpenFileDialog() { Filter = "所有图片文件|*.bmp;*.dib;*.jpg;*.jpeg;*.jpe;*.jfif;*.png|位图文件|*.bmp;*.dib|JPEG|*.jpg;*.jpeg;*.jpe;*.jfif|PNG|*.png" }; while (openFileDialog.ShowDialog() != true) ; var bitmapImage = new BitmapImage(new Uri(openFileDialog.FileName)); Console.WriteLine("Left Margin: "); Console.WriteLine("Top Margin: "); Console.WriteLine("Right Margin: "); Console.WriteLine("Bottom Margin: "); Console.CursorTop = 0; Console.CursorLeft = 13; int leftMargin = int.Parse(Console.ReadLine()); Console.CursorLeft = 12; int topMargin = int.Parse(Console.ReadLine()); Console.CursorLeft = 14; int rightMargin = int.Parse(Console.ReadLine()); Console.CursorLeft = 15; int bottomMargin = int.Parse(Console.ReadLine()); int width = bitmapImage.PixelWidth - leftMargin - rightMargin; int height = bitmapImage.PixelHeight - topMargin - bottomMargin; var writeableBitmap = new WriteableBitmap(width, height, bitmapImage.DpiX, bitmapImage.DpiY, bitmapImage.Format, bitmapImage.Palette); IntPtr backBuffer = writeableBitmap.BackBuffer; bitmapImage.CopyPixels(new Int32Rect(leftMargin, topMargin, width, height), backBuffer, height * writeableBitmap.BackBufferStride, writeableBitmap.BackBufferStride); var saveFileDialog = new SaveFileDialog() { Filter = "所有图片文件|*.bmp;*.dib;*.jpg;*.jpeg;*.jpe;*.jfif;*.png|位图文件|*.bmp;*.dib|JPEG|*.jpg;*.jpeg;*.jpe;*.jfif|PNG|*.png" }; while (saveFileDialog.ShowDialog() != true) ; BitmapEncoder bitmapEncoder = null; if (Regex.IsMatch(saveFileDialog.SafeFileName, @"^.+\.(?:bmp|dib)$")) { bitmapEncoder = new BmpBitmapEncoder(); } else if (Regex.IsMatch(saveFileDialog.SafeFileName, @"^.+\.(?:jpg|jpeg|jpe|jfif)$")) { bitmapEncoder = new JpegBitmapEncoder(); } else if (Regex.IsMatch(saveFileDialog.SafeFileName, @"^.+\.(?:png)$")) { bitmapEncoder = new PngBitmapEncoder(); } bitmapEncoder.Frames.Add(BitmapFrame.Create(writeableBitmap)); var fileStream = new FileStream(saveFileDialog.FileName, FileMode.Create); bitmapEncoder.Save(fileStream); } } }