1. Overview
Sometimes we need to display a map on the web page, for example, a map, which is relatively large. At this time, if we separate a large image into a group of small images, the display speed of the client will obviously feel the block. I hope reading this article will help you.
2. Implementation ideas
. NET Framework GDI + provides a rich set of classes for us to edit graphic images. For more information about. NET Framework GDI +, see the msdn documentation. Here, we will briefly describe the classes used in this program.
The System. Drawing. Image. LoadFile method can be used to create an Image object from a specified file. The System. Drawing. Image. Save method can Save this Image object to a specified file. The System. Drawing. Image. Width and System. Drawing. Image. Height attributes can be used to obtain the Width and Height of an Image.
The System. Drawing. Graphics class can edit images. The System. Drawing. Graphics. DrawImage method draws the specified part of the specified Image object at the specified position and according to the specified size.
Description of image separation: separates a large image into small Blocks Based on the specified width and height.
Tips for beginners: the mathematical coordinates we learned while studying are shown in 2, and the coordinates in GDI + are shown in 3.
3. Implementation Code
1 public class CropImageManipulator
2 {
3 public CropImageManipulator ()
4 {
5
6}
7
8 // file name without extension
9 private string _ fileNameWithoutExtension;
10 // File Extension
11 private string _ fileExtension;
12 // folder to which the file belongs
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 // load the image to be separated
21 Image inputImg = Image. FromFile (inputImgPath );
22 int imgWidth = inputImg. Width;
23 int imgHeight = inputImg. Height;
24
25 // number of cells for Calculation
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 37 {
38 sb. Append ("<tr> ");
39 sb. Append (System. Environment. NewLine );
40 for (int iWidth = 0; iWidth <widthCount; iWidth ++)
41 {
42 // string fileName = " ";
43 string fileName = string. format ("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 newBmp Graphics = Graphics. FromImage (newBmp );
73 newbmp graphics. DrawImage (inputImg, new Rectangle (0, 0, rect. Width, rect. Height), rect, GraphicsUnit. Pixel );
74 newbmp graphics. 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}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service