今天花了一个下午+晚上写的一款简单的图片尺寸调整的小工具,满足不想用图片处理软件的用户(-_-!)

程序思想:导入一个或多个图片文件(JPG,BMP,GIF),按自定义尺寸调整图片大小后,以剪裁或缩放的方式导出后生成所需要的图片文件。
主要代码如下:
窗体包含
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.OpenFileDialog OFD;
private System.Windows.Forms.ListBox FileList;
string[] Files;
private System.Windows.Forms.FolderBrowserDialog FBD;
private System.Windows.Forms.ProgressBar pBar;
private System.Windows.Forms.NumericUpDown picHeight;
private System.Windows.Forms.NumericUpDown picWidth;
private System.Windows.Forms.CheckBox IsCut;
private System.Windows.Forms.PictureBox pictureBox1;
初始化
private void Form1_Load(object sender, System.EventArgs e)

{
InitMenu();
OFD.Multiselect=true;
OFD.Filter="jpg文件|*.jpg|gif文件|*.gif|bmp文件|*.bmp";
button2.Enabled=false;
}
窗体菜单
private void InitMenu()//初始窗体菜单

{
MainMenu mainMenu=new MainMenu();
mainMenu.MenuItems.Add("文件(&F)");
mainMenu.MenuItems.Add("关于(&A)",new EventHandler(About));
mainMenu.MenuItems[0].MenuItems.Add("导入(&I)
",new EventHandler(Import));
mainMenu.MenuItems[0].MenuItems.Add("-");
mainMenu.MenuItems[0].MenuItems.Add("退出(&X)",new EventHandler(Exit));
this.Menu=mainMenu;
}
菜单需要触发的事件
private void Import(object sender, System.EventArgs e)//导入文件

{
OFD.ShowDialog();
Files=OFD.FileNames;
if(Files.Length!=0)

{
for(int i=0;i<Files.Length;i++)

{
FileList.Items.Add(Files[i]);
}
button2.Enabled=true;
}
}
private void Exit(object sender, System.EventArgs e)

{
this.Close();
}
private void About(object sender, System.EventArgs e)

{
MessageBox.Show("简易图片大小调整工具V1.0 制作:随机");
}
导出文件
private void button2_Click(object sender, System.EventArgs e)

{
FBD.ShowDialog();
Bitmap editedPicture;
string savePath=FBD.SelectedPath.Replace("\\","\\\\");
if(FBD.SelectedPath.Trim().Length!=0)//是否选中路径

{
int PictureHeight,PictureWidth;
try

{
for(int i=0;i<FileList.Items.Count;i++)

{
//设置图片的尺寸
PictureWidth=Convert.ToInt16(picWidth.Text);
PictureHeight=Convert.ToInt16(picHeight.Text);

string CurrentFile=FileList.Items[i].ToString();//得到当前文件名
string FileEx=CurrentFile.Substring(CurrentFile.Length-3,3);//取当前文件扩展名
Image img=Image.FromFile(CurrentFile);
if(IsCut.Checked)

{
Bitmap cutPicture=(Bitmap)img;

//判断剪裁范围是否超出图片大小
if(cutPicture.Height<PictureHeight) PictureHeight=cutPicture.Height;
if(cutPicture.Width<PictureWidth) PictureWidth=cutPicture.Width;

editedPicture=cutPicture.Clone(new Rectangle(0,0,PictureWidth,PictureHeight),System.Drawing.Imaging.PixelFormat.Format24bppRgb);//剪裁图片
}
else

{
editedPicture =new Bitmap(img,PictureWidth,PictureHeight);//缩放图片
}
switch(FileEx.ToLower())//按原格式保存

{
case "jpg":editedPicture.Save(savePath+"\\NewPic_"+i.ToString()+".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);break;
case "gif":editedPicture.Save(savePath+"\\NewPic_"+i.ToString()+".gif",System.Drawing.Imaging.ImageFormat.Gif);break;
case "bmp":editedPicture.Save(savePath+"\\NewPic_"+i.ToString()+".bmp",System.Drawing.Imaging.ImageFormat.Bmp);break;
}
Double ProgressValue=(i+1)/Convert.ToDouble(FileList.Items.Count)*100;
pBar.Value=Convert.ToInt16(ProgressValue);//进度处理
}
MessageBox.Show("图片导出完毕!");
pBar.Value=0;
}
catch(Exception Err)

{
MessageBox.Show(Err.Message);
}
}
}
程序难度不大,就是一些简单的应用,当然,这是.net开发的嘛,如果是VC的话,难度就有所提高了:)
程序下载
地址: /Files/YH-Random/PicEdit.rar
程序大小:60KB
运行环境:需.Net Framework支持
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器