程序切图(c#)

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;
   }
  }
 }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值