private void buttonPicChooser_Click(object sender, EventArgs e) { this.progressBarUpload.Value = 0; OpenFileDialog openFileDialogPics = new OpenFileDialog(); openFileDialogPics.InitialDirectory = "D:\\pictest\\orig"; openFileDialogPics.Multiselect = true; openFileDialogPics.Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.gif;*.png"; openFileDialogPics.RestoreDirectory = true; openFileDialogPics.FilterIndex = 1; if (openFileDialogPics.ShowDialog() == DialogResult.OK) { string safePath = openFileDialogPics.FileName.ToString().Replace(openFileDialogPics.SafeFileName.ToString(), ""); this.textBoxPicPath.Text = safePath; for (int i = 0; i < openFileDialogPics.SafeFileNames.Length; i++) { PictureBox picBoxTmp = new PictureBox(); picBoxTmp.Width = 200; picBoxTmp.Height = 150; picBoxTmp.Name = "pictureBoxUpload" + i; string realPic = safePath + openFileDialogPics.SafeFileNames[i].ToString(); this.picsToUpload.Enqueue(realPic); Image imageTmp = new Bitmap(realPic); //非等比例缩放 //picBoxTmp.Image = new Bitmap(imageTmp, 200, 150); //等比例缩放 int h, w; if (imageTmp.Width * picBoxTmp.Height < imageTmp.Height * picBoxTmp.Width) { h = picBoxTmp.Height; w = (int)(imageTmp.Width * 1.0 / imageTmp.Height * picBoxTmp.Height); } else { w = picBoxTmp.Width; h = (int)(imageTmp.Height * 1.0 / imageTmp.Width * picBoxTmp.Width); } bitImg = new Bitmap(imageTmp, w, h); ImageCut IC = new ImageCut(0, 0, picBoxTmp.Width, picBoxTmp.Height); picBoxTmp.Image = IC.KiCut((Bitmap)(this.GetSelectImage(picBoxTmp.Width, picBoxTmp.Height))); imageTmp.Dispose(); picBoxTmp.Visible = true; this.flowLayoutPanelPic.Controls.Add(picBoxTmp); } } }
private void buttonOpenFile_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = "D:\\pictest\\orig"; openFileDialog.Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.gif;*.png"; openFileDialog.RestoreDirectory = true; openFileDialog.FilterIndex = 1; if (openFileDialog.ShowDialog() == DialogResult.OK) { //PicSelector ps = new PicSelector(); //ps.bitOrig = new Bitmap(openFileDialog.FileName.ToString()); //ps.Width = ps.bitOrig.Width; //ps.Height = ps.bitOrig.Height; //ps.Show(); textBoxOrigFile.Text = openFileDialog.FileName.ToString(); //pictureBoxOrig.Image = Image.FromFile(openFileDialog.FileName,true); Image tmp = new Bitmap(openFileDialog.FileName.ToString()); int h, w; if (tmp.Width * pictureBoxOrig.Height < tmp.Height * pictureBoxOrig.Width) { h = this.pictureBoxOrig.Height; w = (int)(tmp.Width * 1.0 / tmp.Height * this.pictureBoxOrig.Height); } else { w = this.pictureBoxOrig.Width; h = (int)(tmp.Height * 1.0 / tmp.Width * this.pictureBoxOrig.Width); } bitImg = new Bitmap(tmp, w, h); //使用打开的图片路径创建位图对像 ImageCut IC = new ImageCut(0,0,this.pictureBoxOrig.Width, this.pictureBoxOrig.Height); //实例化ImageCut类,四个参数据分别表示为:x、y、width、heigth,(40、112)表示pictureBox1的Lcation的坐标,(120、144)表示pictureBox1控件的宽度和高度 pictureBoxOrig.Image = IC.KiCut((Bitmap)(this.GetSelectImage(this.pictureBoxOrig.Width, this.pictureBoxOrig.Height))); //(120、144)表示pictureBox1控件的宽度和高度 //pictureBoxOrig.Image.Save("D:\\pictest\\orig.jpg", ImageFormat.Jpeg); } }