您的位置:首页 > 移动开发 > Objective-C

一个改变图片大小的东东

2007-04-08 16:29 239 查看
Form1.cs

 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


using System.Text.RegularExpressions;




namespace BmpOper




...{


    public partial class Form1 : Form




    ...{


        string picPath;




        public Form1()




        ...{


            InitializeComponent();


            picPath = "";


        }




        private void open_Click(object sender, EventArgs e)




        ...{


            //open source picture and save the path of it to "picPath":




            if (openPicDialog.ShowDialog()== DialogResult.OK)




            ...{


                picPath=openPicDialog.FileName;


  
4000
              showResult.ImageLocation = picPath;  //preview


            }


        }




        private void save_Click(object sender, EventArgs e)




        ...{


            //get Current directory and setup the name of Thumbnail:


            string savePath = picPath.Substring(0, picPath.LastIndexOf(".")) + "Thum.jpg";//get the dir


            


            //Transfer the Function :


            GenerateThum(savePath);


        }




        private void GenerateThum(string savePath)          //Generate the Thumbnail of open picture




        ...{


            if (picPath != "")




            ...{




                Validate the TextBox#region Validate the TextBox




                string tempW = inputWidth.Text;


                string tempH = inputHeigh.Text;


                if (!Regex.IsMatch(tempW, @"^[0-9]+$") && Regex.IsMatch(tempH, @"^[0-9]+$"))




                ...{


                    MessageBox.Show("Error,pleae input a number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);


                    inputWidth.Focus();


                }




                #endregion




                //get the width&height&savepath


                int width = Int32.Parse(inputWidth.Text);


                int height = Int32.Parse(inputHeigh.Text);


                string ThumbPath = savePath;




                //Generate


                Bitmap newBmp = new Bitmap(picPath);


                Image Thumbnail = newBmp.GetThumbnailImage(width,height, null, IntPtr.Zero);




                //Save


                Thumbnail.Save(ThumbPath, System.Drawing.Imaging.ImageFormat.Jpeg);


            }


        }




        private void saveAs_Click(object sender, EventArgs e)       //save as




        ...{


            if (savePicDialog.ShowDialog() == DialogResult.OK)




            ...{


                string savePath = savePicDialog.FileName;


                GenerateThum(savePath);


            }


        }


    }

1871f


}










/**//*


 * Crazy7


 * @ 2007/04/08 pm


 * A program for generate thumbnail of a picture.


*/

 

Form1.Designer.cs:

 


namespace BmpOper




...{


    partial class Form1




    ...{




        /**//// <summary>


        /// Required designer variable.


        /// </summary>


        private System.ComponentModel.IContainer components = null;






        /**//// <summary>


        /// Clean up any resources being used.


        /// </summary>


        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>


        protected override void Dispose(bool disposing)




        ...{


            if (disposing && (components != null))




            ...{


                components.Dispose();


            }


            base.Dispose(disposing);


        }






        Windows Form Designer generated code#region Windows Form Designer generated code






        /**//// <summary>


        /// Required method for Designer support - do not modify


        /// the contents of this method with the code editor.


        /// </summary>


        private void InitializeComponent()




        ...{


            this.open = new System.Windows.Forms.Button();


            this.openPicDialog = new System.Windows.Forms.OpenFileDialog();


            this.showResult = new System.Windows.Forms.PictureBox();


            this.save = new System.Windows.Forms.Button();


            this.inputHeigh = new System.Windows.Forms.TextBox();


            this.inputWidth = new System.Windows.Forms.TextBox();


            this.savePicDialog = new System.Windows.Forms.SaveFileDialog();


            this.saveAs = new System.Windows.Forms.Button();


            this.label1 = new System.Windows.Forms.Label();


            this.label2 = new System.Windows.Forms.Label();


            ((System.ComponentModel.ISupportInitialize)(this.showResult)).BeginInit();


            this.SuspendLayout();


            // 


            // open


            // 


            this.open.Location = new System.Drawing.Point(192, 89);


            this.open.Name = "open";


            this.open.Size = new System.Drawing.Size(94, 24);


            this.open.TabIndex = 0;


            this.open.Text = "Select Picture";


            this.open.UseVisualStyleBackColor = true;


            this.open.Click += new System.EventHandler(this.open_Click);


            // 


            // openPicDialog


            // 


            this.openPicDialog.Filter = "jpg|*.jpg|bmp|*.bmp";


            // 


            // showResult


            // 


            this.showResult.Location = new System.Drawing.Point(12, 89);


            this.showResult.Name = "showResult";


            this.showResult.Size = new System.Drawing.Size(174, 161);


            this.showResult.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;


            this.showResult.TabIndex = 1;


            this.showResult.TabStop = false;


            // 


            // save


            // 


            this.save.Location = new System.Drawing.Point(192, 159);


            this.save.Name = "save";


            this.save.Size = new System.Drawing.Size(94, 24);


            this.save.TabIndex = 2;


            this.save.Text = "Save Picutre";


            this.save.UseVisualStyleBackColor = true;


            this.save.Click += new System.EventHandler(this.save_Click);


            // 


            // inputHeigh


            // 


            this.inputHeigh.Location = new System.Drawing.Point(91, 39);


            this.inputHeigh.Name = "inputHeigh";


            this.inputHeigh.Size = new System.Drawing.Size(41, 23);


            this.inputHeigh.TabIndex = 3;


            this.inputHeigh.Text = "75";


            // 


            // inputWidth


            // 


            this.inputWidth.Location = new System.Drawing.Point(22, 39);


            this.inputWidth.Name = "inputWidth";


            this.inputWidth.Size = new System.Drawing.Size(42, 23);


            this.inputWidth.TabIndex = 4;


            this.inputWidth.Text = "100";


            // 


            // savePicDialog


            // 


            this.savePicDialog.Filter = "jpg|*.jpg";


            // 


            // saveAs


            // 


            this.saveAs.Location = new System.Drawing.Point(192, 227);


            this.saveAs.Name = "saveAs";


            this.saveAs.Size = new System.Drawing.Size(94, 23);


            this.saveAs.TabIndex = 5;


            this.saveAs.Text = "Save As";


            this.saveAs.UseVisualStyleBackColor = true;


            this.saveAs.Click += new System.EventHandler(this.saveAs_Click);


            // 


            // label1


            // 


            this.label1.AutoSize = true;


            this.label1.Location = new System.Drawing.Point(12, 23);


            this.label1.Name = "label1";


            this.label1.Size = new System.Drawing.Size(39, 13);


            this.label1.TabIndex = 6;


            this.label1.Text = "Width:";


            // 


            // label2


            // 


            this.label2.AutoSize = true;


            this.label2.Location = new System.Drawing.Point(79, 23);


            this.label2.Name = "label2";


            this.label2.Size = new System.Drawing.Size(42, 13);


            this.label2.TabIndex = 7;


            this.label2.Text = "Height:";


            // 


            // Form1


            // 


            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);


            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;


            this.ClientSize = new System.Drawing.Size(292, 273);


            this.Controls.Add(this.label2);


            this.Controls.Add(this.label1);


            this.Controls.Add(this.saveAs);


            this.Controls.Add(this.inputWidth);


            this.Controls.Add(this.inputHeigh);


            this.Controls.Add(this.save);


            this.Controls.Add(this.showResult);


            this.Controls.Add(this.open);


            this.Name = "Form1";


            this.Text = "Crazy7-ThumbnailGen";


            ((System.ComponentModel.ISupportInitialize)(this.showResult)).EndInit();


            this.ResumeLayout(false);


            this.PerformLayout();




        }




        #endregion




        private System.Windows.Forms.Button open;


        private System.Windows.Forms.OpenFileDialog openPicDialog;


        private System.Windows.Forms.PictureBox showResult;


        private System.Windows.Forms.Button save;


        private System.Windows.Forms.TextBox inputHeigh;


        private System.Windows.Forms.TextBox inputWidth;


        private System.Windows.Forms.SaveFileDialog savePicDialog;


        private System.Windows.Forms.Button saveAs;


        private System.Windows.Forms.Label label1;


        private System.Windows.Forms.Label label2;


    }


}





 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐