zoukankan      html  css  js  c++  java
  • A Tool to Clip Images

    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);
            }
        }
    }
  • 相关阅读:
    关于IQKeyboardManager的使用
    iOS 关于退出键盘两种方法和避免遮挡
    iOS获取各种数据方法整理以及IDFA与IDFV使用环境
    npm安装模块 -g和--save和--save-dev的区别
    最详细的原生js实现ajax的封装
    js中Math对象常用的属性和方法
    js中的兼容问题汇总
    js中数组方法及分类
    浅析js中的this
    js中的兼容
  • 原文地址:https://www.cnblogs.com/JebediahKerman/p/a_tool_to_clip_images.html
Copyright © 2011-2022 走看看