1. using Microsoft.Win32;
  2. using System;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using System.Windows;
  6. using System.Windows.Media.Imaging;
  7.  
  8. namespace Clip
  9. {
  10. class Clip
  11. {
  12. [STAThread]
  13. static void Main(string[] args)
  14. {
  15. var openFileDialog = new OpenFileDialog()
  16. {
  17. Filter = "所有图片文件|*.bmp;*.dib;*.jpg;*.jpeg;*.jpe;*.jfif;*.png|位图文件|*.bmp;*.dib|JPEG|*.jpg;*.jpeg;*.jpe;*.jfif|PNG|*.png"
  18. };
  19. while (openFileDialog.ShowDialog() != true) ;
  20. var bitmapImage = new BitmapImage(new Uri(openFileDialog.FileName));
  21. Console.WriteLine("Left Margin: ");
  22. Console.WriteLine("Top Margin: ");
  23. Console.WriteLine("Right Margin: ");
  24. Console.WriteLine("Bottom Margin: ");
  25. Console.CursorTop = ;
  26. Console.CursorLeft = ;
  27. int leftMargin = int.Parse(Console.ReadLine());
  28. Console.CursorLeft = ;
  29. int topMargin = int.Parse(Console.ReadLine());
  30. Console.CursorLeft = ;
  31. int rightMargin = int.Parse(Console.ReadLine());
  32. Console.CursorLeft = ;
  33. int bottomMargin = int.Parse(Console.ReadLine());
  34. int width = bitmapImage.PixelWidth - leftMargin - rightMargin;
  35. int height = bitmapImage.PixelHeight - topMargin - bottomMargin;
  36. var writeableBitmap = new WriteableBitmap(width, height, bitmapImage.DpiX, bitmapImage.DpiY, bitmapImage.Format, bitmapImage.Palette);
  37. IntPtr backBuffer = writeableBitmap.BackBuffer;
  38. bitmapImage.CopyPixels(new Int32Rect(leftMargin, topMargin, width, height), backBuffer, height * writeableBitmap.BackBufferStride, writeableBitmap.BackBufferStride);
  39. var saveFileDialog = new SaveFileDialog()
  40. {
  41. Filter = "所有图片文件|*.bmp;*.dib;*.jpg;*.jpeg;*.jpe;*.jfif;*.png|位图文件|*.bmp;*.dib|JPEG|*.jpg;*.jpeg;*.jpe;*.jfif|PNG|*.png"
  42. };
  43. while (saveFileDialog.ShowDialog() != true) ;
  44. BitmapEncoder bitmapEncoder = null;
  45. if (Regex.IsMatch(saveFileDialog.SafeFileName, @"^.+\.(?:bmp|dib)$"))
  46. {
  47. bitmapEncoder = new BmpBitmapEncoder();
  48. }
  49. else if (Regex.IsMatch(saveFileDialog.SafeFileName, @"^.+\.(?:jpg|jpeg|jpe|jfif)$"))
  50. {
  51. bitmapEncoder = new JpegBitmapEncoder();
  52. }
  53. else if (Regex.IsMatch(saveFileDialog.SafeFileName, @"^.+\.(?:png)$"))
  54. {
  55. bitmapEncoder = new PngBitmapEncoder();
  56. }
  57. bitmapEncoder.Frames.Add(BitmapFrame.Create(writeableBitmap));
  58. var fileStream = new FileStream(saveFileDialog.FileName, FileMode.Create);
  59. bitmapEncoder.Save(fileStream);
  60. }
  61. }
  62. }

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. phpcms与discuz的ucenter整合

    1.安装phpcms系统,域名为pc.me   2.安装discuz,并选择安上uc_server,域名为dz.me   3.在phpcms下phpsso的系统设置   4.到ucenter管理中心- ...

  2. 怎样完整地离线更新并升级基于 Debian 的操作系统

    不久之前我已经向你展示了如何在任意离线的 Ubuntu 和 Arch Linux 操作系统上安装软件. 今天,我们将会看看如何完整地离线更新并升级基于 Debian 的操作系统. 和之前所述方法的不同 ...

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

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

  4. 如何使用webpack打包前端项目

    webpack概述 随着前端体积越来越大,功能越来越丰富,这时候就需要将前端工程化,而 webpack就是用于将前端各种文件打包起来. 一个简单的webpack应该包含以下几个概念 · 入口起点 · ...

  5. 经典递归问题:0,1背包问题 kmp 用遗传算法来解背包问题,hash表,位图法搜索,最长公共子序列

    0,1背包问题:我写笔记风格就是想到哪里写哪里,有很多是旧的也没删除,代码内部可能有很多重复的东西,但是保证能运行出最后效果 '''学点高大上的遗传算法''' '''首先是Np问题的定义: npc:多 ...

  6. Linux应用开发入门(转)

    今天偶然看到这篇文章,做个入门了解还是不错的. 前一阵子在QQ上和朋友聊天的时候,总会看到有人说Linux上的应用程序开发是高手才可以完成的,而且这种“迷信”在目前似乎还很普遍.然而,情况并不是这样的 ...

  7. 2018.09.24 bzoj1016: [JSOI2008]最小生成树计数(并查集+搜索)

    传送门 正解是并查集+矩阵树定理. 但由于数据范围小搜索也可以过. 我们需要知道最小生成树的两个性质: 不同的最小生成树中,每种权值的边出现的个数是确定的 不同的生成树中,某一种权值的边连接完成后,形 ...

  8. Django的路由层(2)

    https://www.cnblogs.com/yuanchenqi/articles/8931472.html django2.0版的path Django默认支持以下5个转化器: str,匹配除了 ...

  9. ESP8266-01一些内容

    系统有些指令必须特性状态下才可以执行,即先执行一些其它AT指令 1.设置station模式的IP信息 AT+CIPSTA="192.168.19.1","192.168. ...

  10. c语言中几个常见的库函数strlen、strcmp、strcat、strcpy、strncpy、memset、memcpy、memmove、mmap

    1.strlen() 1)计算给定字符串的长度,不包括’\0’在内 unsigned int strlen(const char *s) { assert(NULL != s);//如果条件不满足,则 ...