C#实现图片分割方法与代码

2016-02-19 11:58 213 1 收藏

下面图老师小编跟大家分享一个简单易学的C#实现图片分割方法与代码教程,get新技能是需要行动的,喜欢的朋友赶紧收藏起来学习下吧!

【 tulaoshi.com - 编程语言 】

1. 概述
有时候我们需要在web页面上显示一张图,比如说一张地图,而这张地图会比较大。这时候如果我们把一张大图分隔成一组小图,那么客户端的显示速度会明显地感觉块。希望阅读本文对你有所帮助。
2. 实现思路
.NET Framework GDI+ 为我们提供了一组丰富地类来编辑图形图像。有关.NET Framework GDI+的详细资料请查阅msdn相关文档。这里只简要叙述本程序要用的的几个类。
System.Drawing.Image.LoadFile 方法可以从指定的文件创建 Image 对象。System.Drawing.Image.Save方法可以将此 Image 对象保存到指定文件。 System.Drawing.Image.Width和System.Drawing.Image.Height属性可以得到图片的宽度和高度。
System.Drawing.Graphics类可以编辑图像。System.Drawing.Graphics.DrawImage方法在指定位置并且按指定大小绘制指定的 Image 对象的指定部分。
图片分隔说明:就是把一张大图,按指定的宽度和高度分隔成一组小块
对初学者的提示:在我们读书时学过的数学坐标如图2所示,在GDI+里的坐标如图3所示
3. 实现代码
 1public class CropImageManipulator
 2    {
 3        public CropImageManipulator()
 4        {
 5            
 6        }
 7
 8        // 不含扩展名的文件名
 9        private string _fileNameWithoutExtension;
10        // 文件扩展名
11        private string _fileExtension;
12        // 文件所属的文件夹
13        private string _fileDirectory;
14        public string Cropping(string inputImgPath, int cropWidth, int cropHeight)
15        {
16            this._fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(inputImgPath);
17            this._fileExtension = System.IO.Path.GetExtension(inputImgPath);
18            this._fileDirectory = System.IO.Path.GetDirectoryName(inputImgPath);
19            
20            // 装载要分隔的图片
21            Image inputImg = Image.FromFile(inputImgPath);
22            int imgWidth = inputImg.Width;
23            int imgHeight = inputImg.Height;
24            
25            // 计算要分几格
26            int widthCount = (int)Math.Ceiling((imgWidth * 1.00) / (cropWidth * 1.00));
27            int heightCount = (int)Math.Ceiling((imgHeight * 1.00) / (cropHeight * 1.00));
28            //----------------------------------------------------------------------
29            ArrayList areaList = new ArrayList();
30            
31            System.Text.StringBuilder sb = new System.Text.StringBuilder();
32            sb.Append("table cellpadding='0' cellspacing='0' border='[$border]'");
33            sb.Append(System.Environment.NewLine);
34
35            int i = 0;
36            for (int iHeight = 0; iHeight  heightCount ; iHeight ++)
37            {
38                sb.Append("tr");
39                sb.Append(System.Environment.NewLine);
40                for (int iWidth = 0; iWidth  widthCount ; iWidth ++)
41                {
42                    //string fileName = "img src='http://localhost/SRcommBeijingFile/"  + this._fileNameWithoutExtension + " _" + i.ToString() + this._fileExtension + "'";
43                    string fileName = string.Format("img src='http://localhost/SRcommBeijingFile/{0}_{1}{2}'  /",this._fileNameWithoutExtension,i,this._fileExtension);
44                    sb.Append("td" + fileName + "/td");
45                    sb.Append(System.Environment.NewLine);
46
47
48                    int pointX = iWidth * cropWidth;
49                    int pointY = iHeight * cropHeight;
50                    int areaWidth = ((pointX + cropWidth)  imgWidth) ? (imgWidth - pointX) : cropWidth;
51                    int areaHeight = ((pointY + cropHeight)  imgHeight) ? (imgHeight - pointY) : cropHeight;
52                    string s = string.Format("{0};{1};{2};{3}",pointX,pointY,areaWidth,areaHeight);
53                    
54                    Rectangle rect = new Rectangle(pointX,pointY,areaWidth,areaHeight);
55                    areaList.Add(rect);
56                    i ++;
57                }
58                sb.Append("/tr");
59                sb.Append(System.Environment.NewLine);
60            }
61
62            sb.Append("/table");
63
64            
65            //----------------------------------------------------------------------    
66            
67            for (int iLoop = 0 ; iLoop  areaList.Count ; iLoop ++)
68            {
69                Rectangle rect = (Rectangle)areaList[iLoop];
70                string fileName = this._fileDirectory + "" + this._fileNameWithoutExtension + "_" + iLoop.ToString() + this._fileExtension;
71                Bitmap newBmp = new Bitmap(rect.Width,rect.Height,PixelFormat.Format24bppRgb);
72                Graphics newBmpGraphics = Graphics.FromImage(newBmp);
73                newBmpGraphics.DrawImage(inputImg,new Rectangle(0,0,rect.Width,rect.Height),rect,GraphicsUnit.Pixel);
74                newBmpGraphics.Save();
75                switch (this._fileExtension.ToLower())
76                {
77                    case ".jpg":
78                    case ".jpeg":
79                        newBmp.Save(fileName,ImageFormat.Jpeg);
80                        break;
81                    case "gif":
82                        newBmp.Save(fileName,ImageFormat.Gif);
83                        break;
84                }
85                
86            }
87            inputImg.Dispose();
88            string html = sb.ToString();
89            return html;
90        }
91
92    }

来源:https://www.tulaoshi.com/n/20160219/1599315.html

延伸阅读
标签: Web开发
解析得到的代码能通过XHTML 1.0 STRICT验证; 包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.  Ubb.ReadMe.htm UBB代码说明标题[h1]标题一[/h1] 标题一 [h2]标题二[/h2] 标题二 [h1]标题三[/h1] 标题三 [h4]标题四[/h4] 标题四 [h5]标题五[/h5] 标题五 [h6]标题六[/h6] 标题六 链接[url]www.unibetter.co...
  using System;  using System.ComponentModel;  using System.Windows.Forms;  using System.Threading;   namespace AutoResetEventTest  {      public partial class Form1 : Form      { &nb...
using System;  using System.Data;  using System.Configuration;  using System.Collections;  using System.Web;  using System.Web.Security;  using System.Web.UI;  using System.Web.UI.WebControls;  using System.Web.UI.WebControls.W...
本文给出一个 C# Mines(布雷)的 代码 ,新手研究一下吧。 以下是引用片段: using System.Collections; using System.IO; using System; namespace com.Mines { class SearchingMines { public ArrayList list = new ArrayList(); pub...
       在我们的开发项目中使用MVC(Model-View-Control)模式的益处是,可以完全降低业务层和应用表示层的相互影响。此外, 我们会有完全独立的对象来操作表示层。MVC在我们项目中提供的这种对象和层之间的独立,将使我们的维护变得更简单使 我们的代码重用变得很容易(下面你将看到)。 ...

经验教程

988

收藏

44
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部