using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace SplitPic
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.TextBox tbSource;
private System.Windows.Forms.Button btnBrowse;
private System.Windows.Forms.Label lblSource;
private System.Windows.Forms.Button btnOutput;
private System.Windows.Forms.Label lblDest;
private System.Windows.Forms.TextBox tbDestPath;
private System.Windows.Forms.Button btnBrowse2;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.tbSource = new System.Windows.Forms.TextBox();
this.btnBrowse = new System.Windows.Forms.Button();
this.lblSource = new System.Windows.Forms.Label();
this.btnOutput = new System.Windows.Forms.Button();
this.lblDest = new System.Windows.Forms.Label();
this.tbDestPath = new System.Windows.Forms.TextBox();
this.btnBrowse2 = new System.Windows.Forms.Button();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.SuspendLayout();
//
// tbSource
//
this.tbSource.Location = new System.Drawing.Point(88, 40);
this.tbSource.Name = "tbSource";
this.tbSource.Size = new System.Drawing.Size(232, 21);
this.tbSource.TabIndex = 0;
this.tbSource.Text = "";
//
// btnBrowse
//
this.btnBrowse.Location = new System.Drawing.Point(344, 40);
this.btnBrowse.Name = "btnBrowse";
this.btnBrowse.TabIndex = 1;
this.btnBrowse.Text = "浏览";
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
//
// lblSource
//
this.lblSource.Location = new System.Drawing.Point(16, 40);
this.lblSource.Name = "lblSource";
this.lblSource.Size = new System.Drawing.Size(64, 23);
this.lblSource.TabIndex = 3;
this.lblSource.Text = "源图位置:";
this.lblSource.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnOutput
//
this.btnOutput.Location = new System.Drawing.Point(24, 168);
this.btnOutput.Name = "btnOutput";
this.btnOutput.TabIndex = 4;
this.btnOutput.Text = "输出小图";
this.btnOutput.Click += new System.EventHandler(this.btnOutput_Click);
//
// lblDest
//
this.lblDest.Location = new System.Drawing.Point(16, 96);
this.lblDest.Name = "lblDest";
this.lblDest.Size = new System.Drawing.Size(64, 23);
this.lblDest.TabIndex = 5;
this.lblDest.Text = "目标位置:";
this.lblDest.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// tbDestPath
//
this.tbDestPath.Location = new System.Drawing.Point(88, 96);
this.tbDestPath.Name = "tbDestPath";
this.tbDestPath.Size = new System.Drawing.Size(232, 21);
this.tbDestPath.TabIndex = 6;
this.tbDestPath.Text = "";
//
// btnBrowse2
//
this.btnBrowse2.Location = new System.Drawing.Point(344, 96);
this.btnBrowse2.Name = "btnBrowse2";
this.btnBrowse2.TabIndex = 7;
this.btnBrowse2.Text = "浏览";
this.btnBrowse2.Click += new System.EventHandler(this.btnBrowse2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 254);
this.Controls.Add(this.btnBrowse2);
this.Controls.Add(this.tbDestPath);
this.Controls.Add(this.lblDest);
this.Controls.Add(this.btnOutput);
this.Controls.Add(this.lblSource);
this.Controls.Add(this.btnBrowse);
this.Controls.Add(this.tbSource);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnBrowse_Click(object sender, System.EventArgs e)
{
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
tbSource.Text = openFileDialog1.FileName;
}
}
private void btnOutput_Click(object sender, System.EventArgs e)
{
// int iStep = 100;
int iStep = 256;
int iLeft = 0, iTop = 0, iWidth = iStep, iHeight = iStep;
// 加载图片
System.Drawing.Image image = new System.Drawing.Bitmap(tbSource.Text);
for (int i = 0; i < image.Height / iStep; i++)
{
iTop = i * iStep;
for (int j = 0; j < image.Width / iStep; j++)
{
iLeft = j * iStep;
// 目标区域
Rectangle destRect = new Rectangle(0, 0, iWidth, iHeight);
// 源图区域
Rectangle srcRect = new Rectangle(iLeft, iTop, iWidth, iHeight);
// 新建Graphics对象
Bitmap newImage = new Bitmap(iWidth, iHeight);
Graphics g = Graphics.FromImage(newImage);
// 绘图平滑程序
g.SmoothingMode = SmoothingMode.HighQuality;
// 图片输出质量
g.CompositingQuality = CompositingQuality.HighQuality;
// 输出到newImage对象
g.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
// 释放绘图对象
g.Dispose();
string strDestFile = string.Format(
// "{0}//l={1}&t={2}&w={3}&h={3}.jpg",
"{0}//x{1}y{2}.jpg",
tbDestPath.Text, iLeft, iTop, iStep
);
newImage.Save(strDestFile);
}
}
// 释放图像资源
image.Dispose();
}
private void btnBrowse2_Click(object sender, System.EventArgs e)
{
DialogResult dr = folderBrowserDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
tbDestPath.Text = folderBrowserDialog1.SelectedPath;
}
}
}
}