C # Implementation of image segmentation method and Code _c# tutorial
Source: Internet
Author: User
1. Overview
Sometimes we need to display a picture on a Web page, such as a map, and this map will be larger. At this point, if we separate a large picture into a group of small graphs, then the client's display speed will obviously feel the block. I hope reading this article is helpful to you.
2. Realize the idea
. NET Framework GDI + provides us with a rich set of classes to edit graphic images. For more information about. NET Framework GDI +, consult the MSDN related documentation. Here is a brief description of some of the classes used in this program.
The System.Drawing.Image.LoadFile method can create an Image object from the specified file. The System.Drawing.Image.Save method can save this Image object to the specified file. The System.Drawing.Image.Width and System.Drawing.Image.Height properties can get the width and height of the picture.
The System.Drawing.Graphics class can edit images. The System.Drawing.Graphics.DrawImage method draws the specified portion of the specified Image object at the specified location and at the specified size.
Picture Separation Description: Is a large picture, according to the specified width and height of a group of small pieces separated
Tips for Beginners: The mathematical coordinates we learned in our reading are shown in Figure 2, and the coordinates in GDI + are shown in Figure 3
3. Implementation code
1public class Cropimagemanipulator
2 {
3 Public Cropimagemanipulator ()
4 {
5
6}
7
8//File name without extension
9 private string _filenamewithoutextension;
10//File name extension
One private string _fileextension;
12//folder to which the file belongs
private string _filedirectory;
public string cropping (string inputimgpath, int cropwidth, int cropheight)
15 {
This._filenamewithoutextension = System.IO.Path.GetFileNameWithoutExtension (Inputimgpath);
This._fileextension = System.IO.Path.GetExtension (Inputimgpath);
This._filedirectory = System.IO.Path.GetDirectoryName (Inputimgpath);
19
20//load the picture to be separated
Image inputimg = Image.FromFile (Inputimgpath);
int imgwidth = Inputimg.width;
int imgheight = Inputimg.height;
24
25//calculation to be divided into several cells
num int widthcount = (int) math.ceiling (imgwidth * 1.00)/(Cropwidth * 1.00));
int heightcount = (int) math.ceiling (imgheight * 1.00)/(Cropheight * 1.00));
/----------------------------------------------------------------------
ArrayList arealist = new ArrayList ();
30
System.Text.StringBuilder sb = new System.Text.StringBuilder ();
SB. Append ("<table cellpadding= ' 0 ' cellspacing= ' 0 ' border= ' [$border] ' >");
SB. Append (System.Environment.NewLine);
34
km int i = 0;
for (int iheight = 0; iheight < Heightcount; iheight + +)
37 {
SB. Append ("<tr>");
SB. Append (System.Environment.NewLine);
for (int iwidth = 0; iwidth < Widthcount; iwidth + +)
41 {
//string fileName = "The string fileName = string. Format ("and SB. Append ("<td>" + fileName + "</td>");
and SB. Append (System.Environment.NewLine);
46
47
+ int pointx = iwidth * cropwidth;
int pointy = iheight * cropheight;
int areawidth = ((Pointx + cropwidth) > ImgWidth)? (IMGWIDTH-POINTX): cropwidth;
Wuyi int areaheight = ((pointy + cropheight) > ImgHeight)? (imgheight-pointy): cropheight;
The string s = String. Format ("{0};{ 1}; {2}; {3} ", Pointx,pointy,areawidth,areaheight);
53
Rectangle rect = new Rectangle (pointx,pointy,areawidth,areaheight);
Arealist.add (rect);
i + +;
57}
and SB. Append ("</tr>");
SB. Append (System.Environment.NewLine);
60}
61
SB. Append ("</table>");
63
64
I//----------------------------------------------------------------------
66
for (int iloop = 0; iloop < Arealist.count; Iloop + +)
68 {
Rectangle rect = (Rectangle) arealist[iloop];
String fileName = This._filedirectory + "\" + this._filenamewithoutextension + "_" + iloop.tostring () + This._fileextension;
Bitmap newbmp = new Bitmap (rect. Width,rect. HEIGHT,PIXELFORMAT.FORMAT24BPPRGB);
Graphics newbmpgraphics = Graphics.fromimage (newbmp);
Newbmpgraphics.drawimage (inputimg,new Rectangle) (0,0,rect. Width,rect. Height), rect,graphicsunit.pixel);
Newbmpgraphics.save ();
Switch (This._fileextension.tolower ())
76 {
Case ". jpg":
Case ". JPEG":
Newbmp.save (Filename,imageformat.jpeg);
The break;
Bayi case "GIF":
Newbmp.save (filename,imageformat.gif);
a break;
84}
85
86}
Inputimg.dispose ();
The string html = sb. ToString ();
The 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