C#實現圖片分割方法與代碼

來源:互聯網
上載者:User

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 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.