自制简易图片尺寸调整工具[源]

本文介绍了一款简单实用的图片尺寸调整工具,支持批量导入JPG、BMP、GIF等格式图片,并按用户自定义尺寸进行剪裁或缩放处理,最后以原格式保存到指定目录。

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



程序思想:导入一个或多个图片文件(JPG,BMP,GIF),按自定义尺寸调整图片大小后,以剪裁或缩放的方式导出后生成所需要的图片文件。

主要代码如下:


窗体包含

None.gif        private  System.Windows.Forms.GroupBox groupBox1;
None.gif        
private  System.Windows.Forms.Button button2;
None.gif        
private  System.Windows.Forms.GroupBox groupBox2;
None.gif        
private  System.Windows.Forms.Label label1;
None.gif        
private  System.Windows.Forms.Label label2;
None.gif        
private  System.Windows.Forms.Label label3;
None.gif        
private  System.Windows.Forms.Label label4;
None.gif        
private  System.Windows.Forms.OpenFileDialog OFD;
None.gif        
private  System.Windows.Forms.ListBox FileList;
None.gif        
string [] Files;
None.gif        
private  System.Windows.Forms.FolderBrowserDialog FBD;
None.gif        
private  System.Windows.Forms.ProgressBar pBar;
None.gif        
private  System.Windows.Forms.NumericUpDown picHeight;
None.gif        
private  System.Windows.Forms.NumericUpDown picWidth;
None.gif        
private  System.Windows.Forms.CheckBox IsCut;
None.gif        
private  System.Windows.Forms.PictureBox pictureBox1;



初始化

None.gif private   void  Form1_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            InitMenu();
InBlock.gif            OFD.Multiselect
=true;
InBlock.gif            OFD.Filter
="jpg文件|*.jpg|gif文件|*.gif|bmp文件|*.bmp";
InBlock.gif            button2.Enabled
=false;
ExpandedBlockEnd.gif        }



窗体菜单

None.gif private   void  InitMenu() // 初始窗体菜单
ExpandedBlockStart.gifContractedBlock.gif
         dot.gif {
InBlock.gif            MainMenu mainMenu
=new MainMenu();
InBlock.gif            mainMenu.MenuItems.Add(
"文件(&F)");
InBlock.gif            mainMenu.MenuItems.Add(
"关于(&A)",new EventHandler(About));
InBlock.gif            mainMenu.MenuItems[
0].MenuItems.Add("导入(&I)dot.gif",new EventHandler(Import));
InBlock.gif            mainMenu.MenuItems[
0].MenuItems.Add("-");
InBlock.gif            mainMenu.MenuItems[
0].MenuItems.Add("退出(&X)",new EventHandler(Exit));
InBlock.gif            
InBlock.gif            
this.Menu=mainMenu;
ExpandedBlockEnd.gif        }



菜单需要触发的事件

None.gif private   void  Import( object  sender, System.EventArgs e) // 导入文件
ExpandedBlockStart.gifContractedBlock.gif
         dot.gif {
InBlock.gif            OFD.ShowDialog();
InBlock.gif            Files
=OFD.FileNames;
InBlock.gif            
if(Files.Length!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int i=0;i<Files.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    FileList.Items.Add(Files[i]);
ExpandedSubBlockEnd.gif                }

InBlock.gif                button2.Enabled
=true;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif        
private   void  Exit( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
this.Close();
ExpandedBlockEnd.gif        }

None.gif       
private   void  About( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            MessageBox.Show(
"简易图片大小调整工具V1.0    制作:随机");
ExpandedBlockEnd.gif        }



导出文件

None.gif private   void  button2_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            FBD.ShowDialog();
InBlock.gif            Bitmap editedPicture;
InBlock.gif            
string savePath=FBD.SelectedPath.Replace("\\","\\\\");
InBlock.gif            
if(FBD.SelectedPath.Trim().Length!=0)//是否选中路径
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                
int PictureHeight,PictureWidth;
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
for(int i=0;i<FileList.Items.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//设置图片的尺寸
InBlock.gif
                        PictureWidth=Convert.ToInt16(picWidth.Text);
InBlock.gif                        PictureHeight
=Convert.ToInt16(picHeight.Text);
InBlock.gif
InBlock.gif                        
string CurrentFile=FileList.Items[i].ToString();//得到当前文件名
InBlock.gif
                        string FileEx=CurrentFile.Substring(CurrentFile.Length-3,3);//取当前文件扩展名
InBlock.gif
                        Image img=Image.FromFile(CurrentFile);
InBlock.gif                        
if(IsCut.Checked)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            Bitmap cutPicture
=(Bitmap)img;
InBlock.gif
InBlock.gif                            
//判断剪裁范围是否超出图片大小
InBlock.gif
                            if(cutPicture.Height<PictureHeight) PictureHeight=cutPicture.Height;
InBlock.gif                            
if(cutPicture.Width<PictureWidth) PictureWidth=cutPicture.Width;
InBlock.gif
InBlock.gif                            editedPicture
=cutPicture.Clone(new Rectangle(0,0,PictureWidth,PictureHeight),System.Drawing.Imaging.PixelFormat.Format24bppRgb);//剪裁图片
ExpandedSubBlockEnd.gif
                        }

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            editedPicture 
=new Bitmap(img,PictureWidth,PictureHeight);//缩放图片
ExpandedSubBlockEnd.gif
                        }

InBlock.gif                        
switch(FileEx.ToLower())//按原格式保存
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        dot.gif{
InBlock.gif                            
case "jpg":editedPicture.Save(savePath+"\\NewPic_"+i.ToString()+".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);break;
InBlock.gif                            
case "gif":editedPicture.Save(savePath+"\\NewPic_"+i.ToString()+".gif",System.Drawing.Imaging.ImageFormat.Gif);break;
InBlock.gif                            
case "bmp":editedPicture.Save(savePath+"\\NewPic_"+i.ToString()+".bmp",System.Drawing.Imaging.ImageFormat.Bmp);break;
ExpandedSubBlockEnd.gif                        }
                                Double ProgressValue=(i+1)/Convert.ToDouble(FileList.Items.Count)*100;
                                pBar.Value=Convert.ToInt16(ProgressValue);//进度处理
InBlock.gif                        
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    MessageBox.Show(
"图片导出完毕!");
InBlock.gif                    pBar.Value
=0;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch(Exception Err)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    MessageBox.Show(Err.Message);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }



程序难度不大,就是一些简单的应用,当然,这是.net开发的嘛,如果是VC的话,难度就有所提高了:)


程序下载


地址: /Files/YH-Random/PicEdit.rar

程序大小:60KB

运行环境:需.Net Framework支持



 

转载于:https://www.cnblogs.com/Random/archive/2007/02/27/658780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值