zoukankan      html  css  js  c++  java
  • 自制简易图片尺寸调整工具[源]

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



    程序思想:导入一个或多个图片文件(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支持



     

  • 相关阅读:
    ZeptoLab Code Rush 2015
    UVa 10048 Audiophobia【Floyd】
    POJ 1847 Tram【Floyd】
    UVa 247 Calling Circles【传递闭包】
    UVa 1395 Slim Span【最小生成树】
    HDU 4006 The kth great number【优先队列】
    UVa 674 Coin Change【记忆化搜索】
    UVa 10285 Longest Run on a Snowboard【记忆化搜索】
    【NOIP2016提高A组模拟9.28】求导
    【NOIP2012模拟10.9】电费结算
  • 原文地址:https://www.cnblogs.com/Random/p/658780.html
Copyright © 2011-2022 走看看