• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#裁剪图片

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

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) +"";
                }
            }
        }
    }
}

界面就是这个鸟样子:

上下左右都是像素值。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap