具体啥的,我也不懂,就知道这样是可以的。等有空了再研究吧。
功能是:裁剪图片,但保持图片原来的大小,被裁剪的区域用指定的颜色(白色)来填充,然后保存到另一个新的文件夹里,名称不改变。
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) +"个"; } } } } }
界面就是这个鸟样子:
上下左右都是像素值。
分类:
.NET
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】