欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)圖片分割方法與代碼

 更新時(shí)間:2007年03月12日 00:00:00   作者:  
1. 概述
有時(shí)候我們需要在web頁(yè)面上顯示一張圖,比如說(shuō)一張地圖,而這張地圖會(huì)比較大。這時(shí)候如果我們把一張大圖分隔成一組小圖,那么客戶端的顯示速度會(huì)明顯地感覺(jué)塊。希望閱讀本文對(duì)你有所幫助。
2. 實(shí)現(xiàn)思路
.NET Framework GDI+ 為我們提供了一組豐富地類來(lái)編輯圖形圖像。有關(guān).NET Framework GDI+的詳細(xì)資料請(qǐng)查閱msdn相關(guān)文檔。這里只簡(jiǎn)要敘述本程序要用的的幾個(gè)類。
System.Drawing.Image.LoadFile 方法可以從指定的文件創(chuàng)建 Image 對(duì)象。System.Drawing.Image.Save方法可以將此 Image 對(duì)象保存到指定文件。 System.Drawing.Image.Width和System.Drawing.Image.Height屬性可以得到圖片的寬度和高度。
System.Drawing.Graphics類可以編輯圖像。System.Drawing.Graphics.DrawImage方法在指定位置并且按指定大小繪制指定的 Image 對(duì)象的指定部分。
圖片分隔說(shuō)明:就是把一張大圖,按指定的寬度和高度分隔成一組小塊
對(duì)初學(xué)者的提示:在我們讀書(shū)時(shí)學(xué)過(guò)的數(shù)學(xué)坐標(biāo)如圖2所示,在GDI+里的坐標(biāo)如圖3所示
3. 實(shí)現(xiàn)代碼
 1public class CropImageManipulator
 2    {
 3        public CropImageManipulator()
 4        {
 5            
 6        }
 7
 8        // 不含擴(kuò)展名的文件名
 9        private string _fileNameWithoutExtension;
10        // 文件擴(kuò)展名
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            // 計(jì)算要分幾格
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    }

相關(guān)文章

  • C#中分部方法和分部類分析

    C#中分部方法和分部類分析

    這篇文章主要介紹了C#中分部方法和分部類基本用法,并且較為詳細(xì)的分析了分部方法和分部類使用時(shí)的注意事項(xiàng),需要的朋友可以參考下
    2014-11-11
  • winform中寫(xiě)app.config文件時(shí)調(diào)試情況下沒(méi)有改變的原因

    winform中寫(xiě)app.config文件時(shí)調(diào)試情況下沒(méi)有改變的原因

    讀取很簡(jiǎn)單基本都用過(guò) ConfigurationManager.AppSettings[""].ToString() 寫(xiě)config不是很常用
    2013-02-02
  • C#支付寶新版支付請(qǐng)求接口調(diào)用

    C#支付寶新版支付請(qǐng)求接口調(diào)用

    這篇文章主要為大家詳細(xì)介紹了C#支付寶新版支付請(qǐng)求接口調(diào)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • C#使用LitJson解析JSON的示例代碼

    C#使用LitJson解析JSON的示例代碼

    本篇文章主要介紹了C#使用LitJson解析JSON的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • Unity2D實(shí)現(xiàn)游戲回旋鏢

    Unity2D實(shí)現(xiàn)游戲回旋鏢

    這篇文章主要為大家詳細(xì)介紹了Unity2D實(shí)現(xiàn)游戲回旋鏢,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • C#泛型集合類型實(shí)現(xiàn)添加和遍歷

    C#泛型集合類型實(shí)現(xiàn)添加和遍歷

    這篇文章介紹了C#泛型集合類型實(shí)現(xiàn)添加和遍歷的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • C#實(shí)現(xiàn)winform用子窗體刷新父窗體及子窗體改變父窗體控件值的方法

    C#實(shí)現(xiàn)winform用子窗體刷新父窗體及子窗體改變父窗體控件值的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)winform用子窗體刷新父窗體及子窗體改變父窗體控件值的方法,涉及C#窗體交互的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • C#實(shí)現(xiàn)飛行棋優(yōu)化版

    C#實(shí)現(xiàn)飛行棋優(yōu)化版

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)飛行棋的優(yōu)化版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • C#讀取文本文件到listbox組件的方法

    C#讀取文本文件到listbox組件的方法

    這篇文章主要介紹了C#讀取文本文件到listbox組件的方法,涉及C#操作文本文件及l(fā)istbox組件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C#多線程系列之線程等待

    C#多線程系列之線程等待

    本文詳細(xì)講解了C#多線程中的線程等待,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02

最新評(píng)論