具体啥的,我也不懂,就知道这样是可以的。等有空了再研究吧。

功能是:裁剪图片,但保持图片原来的大小,被裁剪的区域用指定的颜色(白色)来填充,然后保存到另一个新的文件夹里,名称不改变。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WFImg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                this.textBox1.Text = foldPath;
            }
        }

        /// <summary>
        /// 剪裁
        /// </summary>
        /// <param name="b">原始Bitmap</param>
        /// <param name="StartX">开始坐标X</param>
        /// <param name="StartY">开始坐标Y</param>
        /// <param name="iWidth">宽度</param>
        /// <param name="iHeight">高度</param>
        /// <returns>剪裁后的Bitmap</returns>
        public static Bitmap KiCut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
        {
            if (b == null)
            {
                return null;
            }

            int w = b.Width;
            int h = b.Height;
            if (StartX >= w || StartY >= h)
            {
                return null;
            }

            if (StartX + iWidth > w)
            {
                iWidth = w - StartX;
            }

            if (StartY + iHeight > h)
            {
                iHeight = h - StartY;
            }
            try
            {
                //Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);//裁剪成小一号的图片

                Bitmap bmpOut = new Bitmap(w, h, PixelFormat.Format24bppRgb);//保持原大小,裁剪部分用指定的颜色填充
                Graphics graphics = Graphics.FromImage(bmpOut);
                graphics.Clear(Color.White);//裁剪部分用指定的颜色填充
                graphics.DrawImage(b, new Rectangle(StartX, StartY, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
                graphics.Dispose();
                return bmpOut;
            }
            catch
            {
                return null;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int leftSize = Convert.ToInt32(txtLeft.Text);
            int topSize = Convert.ToInt32(txtTop.Text);
            int bottomSize = Convert.ToInt32(txtBottom.Text);
            int rightSize = Convert.ToInt32(txtRight.Text);

            string foldPath = textBox1.Text;
            string newPath = foldPath + "_new";
            if (!Directory.Exists(newPath))
            {
                Directory.CreateDirectory(newPath);
            }
            //循环该目录下的文件
            DirectoryInfo dir = new DirectoryInfo(foldPath);
            FileInfo[] files = dir.GetFiles();
            int fcount = files.Count();
            if (fcount > 0)
            {
                for (int i = 0; i < fcount; i++)
                {
                    FileInfo file = files[i];
                    Bitmap map = new Bitmap(file.FullName);
                    int width = map.Width;
                    int height = map.Height;

                    map = KiCut(map, leftSize, topSize, width - rightSize - leftSize, height - bottomSize - topSize);

                    map.Save(newPath + "\\" + file.Name);

                    label6.Text = "执行结果:成功" +(i + 1) +"";
                }
            }
        }
    }
}

界面就是这个鸟样子:

上下左右都是像素值。

posted on 2017-11-10 14:03  邢帅杰  阅读(1433)  评论(0编辑  收藏  举报