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 ...{ publicpartialclass Form1 : Form ...{ string picPath; public Form1() ...{ InitializeComponent(); picPath =""; } privatevoid 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; showResult.ImageLocation = picPath; //preview } } privatevoid 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); } privatevoid 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); } } privatevoid saveAs_Click(object sender, EventArgs e) //save as ...{ if (savePicDialog.ShowDialog() == DialogResult.OK) ...{ string savePath = savePicDialog.FileName; GenerateThum(savePath); } } } } /**//* * Crazy7 * @ 2007/04/08 pm * A program for generate thumbnail of a picture. */