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 = ;
Console.CursorLeft = ;
int leftMargin = int.Parse(Console.ReadLine());
Console.CursorLeft = ;
int topMargin = int.Parse(Console.ReadLine());
Console.CursorLeft = ;
int rightMargin = int.Parse(Console.ReadLine());
Console.CursorLeft = ;
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);
}
}
}

A Tool to Clip Images的更多相关文章

  1. 基于2d Tool Kit 精灵合图,动作生成工具

    http://blog.csdn.net/onerain88/article/details/18563687 2d Tool Kit 是一款出色的基于unity3d 开发2d游戏的工具,提供了丰富的 ...

  2. How to Write Doc Comments for the Javadoc Tool

    http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html This document describe ...

  3. How to installation V145 Renault CAN Clip diagnostic software

    Eobd2.fr has launched the new 2015 V145 Renault CAN Clip diagnostic tool (SP19-A and SP19-B). Here i ...

  4. [CSS] Useful CSS tool for Web designer and developer

    1. Color Picker (Chrome) You might know how to use color picker in Chrome, recently there is a featu ...

  5. Split Animation Clip From FBX and Multiply Mode Sprite

    Use Script To Creat 2D Animation Clip From Multiply Mode Sprite 很多时候美工拿过来的一张序列帧图片,我们需要转换成 Multiply M ...

  6. [免费了] SailingEase .NET Resources Tool (.NET 多语言资源编辑器)

    这是我2010年左右,写 Winform IDE (http://www.cnblogs.com/sheng_chao/p/4387249.html)项目时延伸出的一个小项目. 最初是以共享软件的形式 ...

  7. [Tool] github 入手教程

    简单的介绍一下 Github 的基本操作. 主页:https://github.com/ 首先自然是在 GitHub 注册一个帐号了.然后开始正文吧. Git 基本介绍 Git 是属于分布式版本控制系 ...

  8. jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the install tool.

    jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the ...

  9. mtk flash tool,Win7 On VirtualBox

    SP_Flash_Tool_exe_Windows_v5.1624.00.000 Win7 在 VirtualBox, 安裝 mtk flash tool, v5.1628 在燒錄時會 fail. v ...

随机推荐

  1. struts框架值栈问题七之EL表达式也会获取到值栈中的数据

    7. 问题七:为什么EL也能访问值栈中的数据? * StrutsPreparedAndExecuteFilter的doFilter代码中 request = prepare.wrapRequest(r ...

  2. Html使用Iframe无刷新上传文件,后台接收

    html代码:我是发送请求到teacher_center.aspx,不是到.ashx一般处理程序里,需要加 runat="server",有空我再试试发送请求到 .ashx 里 & ...

  3. Apache Cordova vs Adobe PhoneGap: the differences and which one to use

    http://www.makehybridapps.com/2014/06/09/cordova-vs-phonegap-the-differences-and-which-one-to-use/

  4. linux下安装oracle数据库详细教程

    一.安装yum源 下载或拷贝RedHat的iso镜像到本地,比如 /repo/iso/ rhel-server-6.6-x86_64-dvd.iso 1.建立ISO文件存放目录(/repo/iso)和 ...

  5. Laravel 的 Events(事件) 及 Observers(观察者)

    你是否听说过单一职责原则(single responsibility principle)?我希望是的.它是程序设计的基本原则之一,它基本上的意思就是,一个类有且只有一个职责.换句话说,一个类必须且只 ...

  6. [SoapUI] Tips and Tricks(提示和技巧)

    https://www.soapui.org/scripting-properties/tips-tricks.html

  7. 2018.09.11 bzoj47214721: [Noip2016]蚯蚓(单调队列)

    传送门 好题. 目测只会多带一个log2(n+m)" role="presentation" style="position: relative;"& ...

  8. 8b10b

    目的:保持直流平衡DC Balance). running disparity() 8bit原始数据会分成两部分,其低5位会进行5B/6B编码,高3位则进行3B/4B编码,这两种映射关系在当时已经成为 ...

  9. AE IRasterCursor 获取栅格图层像素值

    在编写使用栅格图层的代码时,常常要获取栅格图层的像素值(PixelValue).如果想获取某一点的像素值,可以使用IRaster2中的getPixelValue方法.但如果想要获得的是图层中的某一块甚 ...

  10. SqlCommand和SqlDataAdapter的区别

    SqlDataAdapter对象 一.特点介绍1.表示用于填充 DataSet 和更新 SQL Server 数据库的一组数据命令和一个数据库连接.2.在SqlDataAdapter和DataSet之 ...