1. Overview
Sometimes we need to display a picture on the web page, such as a map, and this map will be relatively large. At this time, if we divide a large image into a group of small images, the display speed of the client will obviously feel blocked. Hope reading this article will be helpful to you.
2. Implementation ideas
.NET Framework GDI+ provides us with a rich set of classes to edit graphic images. For detailed information about .NET Framework GDI+, please refer to the msdn-related documents. Here we will briefly describe the several categories that this program needs to use.
Methods can create Image object from the specified file. Methods can save this Image object to the specified file. and attributes can get the width and height of the picture.
Classes can edit images. The method draws the specified part of the specified Image object at the specified location and at the specified size.
Image separation instructions: It is to divide a large image into a group of small pieces according to the specified width and height.
Tips for beginners: The mathematical coordinates we learned when we were studying are shown in Figure 2, and the coordinates in GDI+ are shown in Figure 3
3. Implement code
1public class CropImageManipulator
2 {
3 public CropImageManipulator()
4 {
5
6 }
7
8 // Filenames without extension
9 private string _fileNameWithoutExtension;
10 // File extension
11 private string _fileExtension;
12 // The folder to which the file belongs
13 private string _fileDirectory;
14 public string Cropping(string inputImgPath, int cropWidth, int cropHeight)
15 {
16 this._fileNameWithoutExtension = (inputImgPath);
17 this._fileExtension = (inputImgPath);
18 this._fileDirectory = (inputImgPath);
19
20 // Load the pictures to be separated
21 Image inputImg = (inputImgPath);
22 int imgWidth = ;
23 int imgHeight = ;
24
25
26 int widthCount = (int)((imgWidth * 1.00) / (cropWidth * 1.00));
27 int heightCount = (int)((imgHeight * 1.00) / (cropHeight * 1.00));
28 //----------------------------------------------------------------------
29 ArrayList areaList = new ArrayList();
30
31 sb = new ();
32 ("<table cellpadding='0' cellspacing='0' border='[$border]'>");
33 ();
34
35 int i = 0;
36 for (int iHeight = 0; iHeight < heightCount ; iHeight ++)
37 {
38 ("<tr>");
39 ();
40 for (int iWidth = 0; iWidth < widthCount ; iWidth ++)
41 {
42 //string fileName = "<img src='http://localhost/SRcommBeijingFile/" + this._fileNameWithoutExtension + " _" + () + this._fileExtension + "'>";
43 string fileName = ("<img src='http://localhost/SRcommBeijingFile/{0}_{1}{2}' />",this._fileNameWithoutExtension,i,this._fileExtension);
44 ("<td>" + fileName + "</td>");
45 ();
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 = ("{0};{1};{2};{3}",pointX,pointY,areaWidth,areaHeight);
53
54 Rectangle rect = new Rectangle(pointX,pointY,areaWidth,areaHeight);
55 (rect);
56 i ++;
57 }
58 ("</tr>");
59 ();
60 }
61
62 ("</table>");
63
64
65 //----------------------------------------------------------------------
66
67 for (int iLoop = 0 ; iLoop < ; iLoop ++)
68 {
69 Rectangle rect = (Rectangle)areaList[iLoop];
70 string fileName = this._fileDirectory + "\\" + this._fileNameWithoutExtension + "_" + () + this._fileExtension;
71 Bitmap newBmp = new Bitmap(,,PixelFormat.Format24bppRgb);
72 Graphics newBmpGraphics = (newBmp);
73 (inputImg,new Rectangle(0,0,,),rect,);
74 ();
75 switch (this._fileExtension.ToLower())
76 {
77 case ".jpg":
78 case ".jpeg":
79 (fileName,);
80 break;
81 case "gif":
82 (fileName,);
83 break;
84 }
85
86 }
87 ();
88 string html = ();
89 return html;
90 }
91
92 }
Sometimes we need to display a picture on the web page, such as a map, and this map will be relatively large. At this time, if we divide a large image into a group of small images, the display speed of the client will obviously feel blocked. Hope reading this article will be helpful to you.
2. Implementation ideas
.NET Framework GDI+ provides us with a rich set of classes to edit graphic images. For detailed information about .NET Framework GDI+, please refer to the msdn-related documents. Here we will briefly describe the several categories that this program needs to use.
Methods can create Image object from the specified file. Methods can save this Image object to the specified file. and attributes can get the width and height of the picture.
Classes can edit images. The method draws the specified part of the specified Image object at the specified location and at the specified size.
Image separation instructions: It is to divide a large image into a group of small pieces according to the specified width and height.
Tips for beginners: The mathematical coordinates we learned when we were studying are shown in Figure 2, and the coordinates in GDI+ are shown in Figure 3
3. Implement code
1public class CropImageManipulator
2 {
3 public CropImageManipulator()
4 {
5
6 }
7
8 // Filenames without extension
9 private string _fileNameWithoutExtension;
10 // File extension
11 private string _fileExtension;
12 // The folder to which the file belongs
13 private string _fileDirectory;
14 public string Cropping(string inputImgPath, int cropWidth, int cropHeight)
15 {
16 this._fileNameWithoutExtension = (inputImgPath);
17 this._fileExtension = (inputImgPath);
18 this._fileDirectory = (inputImgPath);
19
20 // Load the pictures to be separated
21 Image inputImg = (inputImgPath);
22 int imgWidth = ;
23 int imgHeight = ;
24
25
26 int widthCount = (int)((imgWidth * 1.00) / (cropWidth * 1.00));
27 int heightCount = (int)((imgHeight * 1.00) / (cropHeight * 1.00));
28 //----------------------------------------------------------------------
29 ArrayList areaList = new ArrayList();
30
31 sb = new ();
32 ("<table cellpadding='0' cellspacing='0' border='[$border]'>");
33 ();
34
35 int i = 0;
36 for (int iHeight = 0; iHeight < heightCount ; iHeight ++)
37 {
38 ("<tr>");
39 ();
40 for (int iWidth = 0; iWidth < widthCount ; iWidth ++)
41 {
42 //string fileName = "<img src='http://localhost/SRcommBeijingFile/" + this._fileNameWithoutExtension + " _" + () + this._fileExtension + "'>";
43 string fileName = ("<img src='http://localhost/SRcommBeijingFile/{0}_{1}{2}' />",this._fileNameWithoutExtension,i,this._fileExtension);
44 ("<td>" + fileName + "</td>");
45 ();
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 = ("{0};{1};{2};{3}",pointX,pointY,areaWidth,areaHeight);
53
54 Rectangle rect = new Rectangle(pointX,pointY,areaWidth,areaHeight);
55 (rect);
56 i ++;
57 }
58 ("</tr>");
59 ();
60 }
61
62 ("</table>");
63
64
65 //----------------------------------------------------------------------
66
67 for (int iLoop = 0 ; iLoop < ; iLoop ++)
68 {
69 Rectangle rect = (Rectangle)areaList[iLoop];
70 string fileName = this._fileDirectory + "\\" + this._fileNameWithoutExtension + "_" + () + this._fileExtension;
71 Bitmap newBmp = new Bitmap(,,PixelFormat.Format24bppRgb);
72 Graphics newBmpGraphics = (newBmp);
73 (inputImg,new Rectangle(0,0,,),rect,);
74 ();
75 switch (this._fileExtension.ToLower())
76 {
77 case ".jpg":
78 case ".jpeg":
79 (fileName,);
80 break;
81 case "gif":
82 (fileName,);
83 break;
84 }
85
86 }
87 ();
88 string html = ();
89 return html;
90 }
91
92 }