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);
            }
        }
    }
  • 相关阅读:
    消息队列 资源不足,无法执行操作
    内存级的缓存实际上引用
    Vs 2013 单步调试 .net framework 中遇到的问题
    Win7总是显示“软件应用无法兼容”的解决方法
    Win10系统文件受损怎么办
    教你win10系统如何一键修复系统
    Win10专业版如何提升游戏流畅度
    win7电脑任务管理器被停用如何解决
    win7系统移动硬盘打不开解决方法
    Java之集合(五)LinkedList
  • 原文地址:https://www.cnblogs.com/JebediahKerman/p/a_tool_to_clip_images.html
Copyright © 2011-2022 走看看