1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Drawing.Imaging; 11 using System.IO; 12 13 namespace picfg 14 { 15 public partial class Form1 : Form 16 { 17 public Form1() 18 { 19 InitializeComponent(); 20 } 21 22 private void button1_Click(object sender, EventArgs e) 23 { 24 25 openFileDialog1.ShowDialog(); 26 textBox1.Text = openFileDialog1.FileName; 27 if (openFileDialog1.FileName != "") 28 { 29 pictureBox1.Load(openFileDialog1.FileName); 30 label7.Text = pictureBox1.Image.Width.ToString() + " x " + pictureBox1.Image.Height.ToString(); 31 button2.Enabled = true; 32 } 33 34 } 35 string folder=""; 36 private void button2_Click(object sender, EventArgs e) 37 { 38 39 if (!(textBox4.Text == "" || textBox3.Text == "" || textBox5.Text == "")) 40 { 41 42 int w, h, x, y; 43 int n = int.Parse(textBox2.Text); //行 44 int m = int.Parse(textBox3.Text); //列 45 int index = int.Parse(textBox5.Text); 46 string file; 47 w = pictureBox1.Image.Width / m; 48 h = pictureBox1.Image.Height / n; 49 50 for (int i = 0; i < n; i++) 51 { 52 for (int j = 0; j < m; j++) 53 { 54 y = i * h; 55 x = j * w; 56 string dir = Application.StartupPath + "\\" + textBox4.Text + "\\"; 57 folder = dir; 58 if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); 59 file = Application.StartupPath + "\\" + textBox4.Text + "\\" + textBox4.Text + (index + i * m + j) + comboBox1.Text; 60 61 picjq(pictureBox1.Image, w, h, x, y, file, comboBox1.Text); 62 63 64 } 65 } 66 MessageBox.Show("分割完成"); 67 } 68 else MessageBox.Show("编辑框不能为空,请填写完整!"); 69 70 } 71 72 73 74 75 private void picjq(Image img,int w,int h,int x,int y,string file,string type) 76 { 77 Bitmap bitmap = new Bitmap(w, h); //创建新图位图 78 Graphics graphics = Graphics.FromImage(bitmap); //创建作图区域 79 graphics.DrawImage(img, new Rectangle(0, 0, w, h), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);//截取原图相应区域写入作图区 80 Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap()); //从作图区生成新图 81 // pictureBox1.Image = saveImage; 82 //保存图象 83 switch(type) 84 { 85 case ".jpg": saveImage.Save(file, ImageFormat.Jpeg); break; 86 case ".gif": saveImage.Save(file, ImageFormat.Gif); break; 87 case ".bmp": saveImage.Save(file, ImageFormat.Bmp); break; 88 case ".png": saveImage.Save(file, ImageFormat.Png); break; 89 default: saveImage.Save(file, ImageFormat.Jpeg); break; 90 91 } 92 saveImage.Dispose(); //释放 93 bitmap.Dispose(); 94 graphics.Dispose(); 95 96 97 98 } 99 //限定textbox只能输入数字100 private void textBox2_TextChanged(object sender, EventArgs e)101 {102 if (textBox2.Text!="")label8.Text = "将被分割成" + textBox2.Text + "行";103 else label8.Text = "";104 }105 106 private void textBox2_KeyPress(object sender, KeyPressEventArgs e)107 {108 e.Handled = e.KeyChar < '1' || e.KeyChar > '9'; 109 if (e.KeyChar == (char)8) e.Handled = false;110 }111 112 private void textBox3_KeyPress(object sender, KeyPressEventArgs e)113 {114 e.Handled = e.KeyChar < '0' || e.KeyChar > '9';115 if (e.KeyChar == (char)8) e.Handled = false;116 117 }118 119 private void textBox5_KeyPress(object sender, KeyPressEventArgs e)120 {121 e.Handled = e.KeyChar < '0' || e.KeyChar > '9';122 if (e.KeyChar == (char)8) e.Handled = false;123 124 }125 126 private void textBox3_TextChanged(object sender, EventArgs e)127 {128 if (textBox3.Text != "") label9.Text = "将被分割成" + textBox3.Text + "列";129 else label9.Text = "";130 }131 132 private void button3_Click(object sender, EventArgs e)133 {134 if(folder!="")System.Diagnostics.Process.Start("Explorer.exe",folder);135 else System.Diagnostics.Process.Start("Explorer.exe", Application.StartupPath);136 }137 138 139 140 141 142 143 144 145 }146 }
百度云