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);
            }
        }
    }
  • 相关阅读:
    selenium——上传文件
    selenium——下拉框
    selenium——鼠标操作ActionChains:点击、滑动、拖动
    selenium定位元素—逻辑运算、xpath函数、轴定位
    spring设置webAppRootKey
    树莓3 集群
    redis集群搭建
    MQ基础
    MQ集群测试环境搭建(多节点负载均衡,共享一个kahaDB文件(nas方式))
    weblogic对jms实现的QueueConnection实现与TopicConnection实现问题
  • 原文地址:https://www.cnblogs.com/JebediahKerman/p/a_tool_to_clip_images.html
Copyright © 2011-2022 走看看