您的位置:首页 > 其它

【曾经】图片快速分割

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



百度云下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: