博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【曾经】图片快速分割
阅读量:5341 次
发布时间:2019-06-15

本文共 5103 字,大约阅读时间需要 17 分钟。

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 }
View Code

百度云

 

转载于:https://www.cnblogs.com/tyks/p/4875775.html

你可能感兴趣的文章
NYOJ458 - 小光棍数
查看>>
java中常用方法
查看>>
【Programming Clip】06、07年清华计算机考研上机试题解答(个别测试用例无法通过)...
查看>>
canvas动画
查看>>
4,7周围玩家
查看>>
关于webpack升级过后不能打包的问题;
查看>>
vue - 生命周期
查看>>
Python正则表达式
查看>>
Linux进程间通信--命名管道
查看>>
UVa 10970 - Big Chocolate
查看>>
js输出
查看>>
H5多文本换行
查看>>
HAL层三类函数及其作用
查看>>
Odoo 去掉 恼人的 "上午"和"下午"
查看>>
web@h,c小总结
查看>>
java编程思想笔记(一)——面向对象导论
查看>>
Data Structure 基本概念
查看>>
Ubuntu改坏sudoers后无法使用sudo的解决办法
查看>>
NEYC 2017 游记
查看>>
[搬运] 写给 C# 开发人员的函数式编程
查看>>