【曾经】图片快速分割
2015-10-13 20:40
190 查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing.Imaging; using System.IO; namespace picfg { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { openFileDialog1.ShowDialog(); textBox1.Text = openFileDialog1.FileName; if (openFileDialog1.FileName != "") { pictureBox1.Load(openFileDialog1.FileName); label7.Text = pictureBox1.Image.Width.ToString() + " x " + pictureBox1.Image.Height.ToString(); button2.Enabled = true; } } string folder=""; private void button2_Click(object sender, EventArgs e) { if (!(textBox4.Text == "" || textBox3.Text == "" || textBox5.Text == "")) { int w, h, x, y; int n = int.Parse(textBox2.Text); //行 int m = int.Parse(textBox3.Text); //列 int index = int.Parse(textBox5.Text); string file; w = pictureBox1.Image.Width / m; h = pictureBox1.Image.Height / n; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { y = i * h; x = j * w; string dir = Application.StartupPath + "\\" + textBox4.Text + "\\"; folder = dir; if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); file = Application.StartupPath + "\\" + textBox4.Text + "\\" + textBox4.Text + (index + i * m + j) + comboBox1.Text; picjq(pictureBox1.Image, w, h, x, y, file, comboBox1.Text); } } MessageBox.Show("分割完成"); } else MessageBox.Show("编辑框不能为空,请填写完整!"); } private void picjq(Image img,int w,int h,int x,int y,string file,string type) { Bitmap bitmap = new Bitmap(w, h); //创建新图位图 Graphics graphics = Graphics.FromImage(bitmap); //创建作图区域 graphics.DrawImage(img, new Rectangle(0, 0, w, h), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);//截取原图相应区域写入作图区 Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap()); //从作图区生成新图 // pictureBox1.Image = saveImage; //保存图象 switch(type) { case ".jpg": saveImage.Save(file, ImageFormat.Jpeg); break; case ".gif": saveImage.Save(file, ImageFormat.Gif); break; case ".bmp": saveImage.Save(file, ImageFormat.Bmp); break; case ".png": saveImage.Save(file, ImageFormat.Png); break; default: saveImage.Save(file, ImageFormat.Jpeg); break; } saveImage.Dispose(); //释放 bitmap.Dispose(); graphics.Dispose(); } //限定textbox只能输入数字 private void textBox2_TextChanged(object sender, EventArgs e) { if (textBox2.Text!="")label8.Text = "将被分割成" + textBox2.Text + "行"; else label8.Text = ""; } private void textBox2_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = e.KeyChar < '1' || e.KeyChar > '9'; if (e.KeyChar == (char)8) e.Handled = false; } private void textBox3_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = e.KeyChar < '0' || e.KeyChar > '9'; if (e.KeyChar == (char)8) e.Handled = false; } private void textBox5_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = e.KeyChar < '0' || e.KeyChar > '9'; if (e.KeyChar == (char)8) e.Handled = false; } private void textBox3_TextChanged(object sender, EventArgs e) { if (textBox3.Text != "") label9.Text = "将被分割成" + textBox3.Text + "列"; else label9.Text = ""; } private void button3_Click(object sender, EventArgs e) { if(folder!="")System.Diagnostics.Process.Start("Explorer.exe",folder); else System.Diagnostics.Process.Start("Explorer.exe", Application.StartupPath); } } }
View Code
百度云下载
相关文章推荐
- WaitForSingleObject的用法
- 深入浅出Android App耗电量统计
- iframe父级与iframe子级间访问
- IK Analyzer demo
- IOS contentMode属性
- 自定义进度条和RatingBar评分控件
- Spring 4.0 + Spring MVC(四)学习
- TCP的拥塞控制
- Oracle导出表(即DMP文件)的两种方法
- 《网络编程》先进 I/O
- 1011. A+B和C (15)
- Swap Nodes in Pairs
- 21总结
- C++关键字的详解 ---- mutable关键字
- 折半查找
- c#入门经典1~4章 与c++的对比
- 中国计算机学会推荐国际学术刊物 会议和期刊目录
- fork, vfork, clone, exec的区别
- spring系列之3--iBATIS的使用
- 类神经网络的角点检测方法