手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#实现图片分割方法与代码
C#实现图片分割方法与代码
摘要:1.概述有时候我们需要在web页面上显示一张图,比如说一张地图,而这张地图会比较大。这时候如果我们把一张大图分隔成一组小图,那么客户端的显示...

1.概述

有时候我们需要在web页面上显示一张图,比如说一张地图,而这张地图会比较大。这时候如果我们把一张大图分隔成一组小图,那么客户端的显示速度会明显地感觉块。希望阅读本文对你有所帮助。

2.实现思路

.NETFrameworkGDI+为我们提供了一组丰富地类来编辑图形图像。有关.NETFrameworkGDI+的详细资料请查阅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.实现代码

1publicclassCropImageManipulator

2{

3publicCropImageManipulator()

4{

5

6}

7

8//不含扩展名的文件名

9privatestring_fileNameWithoutExtension;

10//文件扩展名

11privatestring_fileExtension;

12//文件所属的文件夹

13privatestring_fileDirectory;

14publicstringCropping(stringinputImgPath,intcropWidth,intcropHeight)

15{

16this._fileNameWithoutExtension=System.IO.Path.GetFileNameWithoutExtension(inputImgPath);

17this._fileExtension=System.IO.Path.GetExtension(inputImgPath);

18this._fileDirectory=System.IO.Path.GetDirectoryName(inputImgPath);

19

20//装载要分隔的图片

21ImageinputImg=Image.FromFile(inputImgPath);

22intimgWidth=inputImg.Width;

23intimgHeight=inputImg.Height;

24

25//计算要分几格

26intwidthCount=(int)Math.Ceiling((imgWidth*1.00)/(cropWidth*1.00));

27intheightCount=(int)Math.Ceiling((imgHeight*1.00)/(cropHeight*1.00));

28//----------------------------------------------------------------------

29ArrayListareaList=newArrayList();

30

31System.Text.StringBuildersb=newSystem.Text.StringBuilder();

32sb.Append("<tablecellpadding='0'cellspacing='0'border='[$border]'>");

33sb.Append(System.Environment.NewLine);

34

35inti=0;

36for(intiHeight=0;iHeight<heightCount;iHeight++)

37{

38sb.Append("<tr>");

39sb.Append(System.Environment.NewLine);

40for(intiWidth=0;iWidth<widthCount;iWidth++)

41{

42//stringfileName="<imgsrc='http://localhost/SRcommBeijingFile/"+this._fileNameWithoutExtension+"_"+i.ToString()+this._fileExtension+"'>";

43stringfileName=string.Format("<imgsrc='http://localhost/SRcommBeijingFile/{0}_{1}{2}'/>",this._fileNameWithoutExtension,i,this._fileExtension);

44sb.Append("<td>"+fileName+"</td>");

45sb.Append(System.Environment.NewLine);

46

47

48intpointX=iWidth*cropWidth;

49intpointY=iHeight*cropHeight;

50intareaWidth=((pointX+cropWidth)>imgWidth)?(imgWidth-pointX):cropWidth;

51intareaHeight=((pointY+cropHeight)>imgHeight)?(imgHeight-pointY):cropHeight;

52strings=string.Format("{0};{1};{2};{3}",pointX,pointY,areaWidth,areaHeight);

53

54Rectanglerect=newRectangle(pointX,pointY,areaWidth,areaHeight);

55areaList.Add(rect);

56i++;

57}

58sb.Append("</tr>");

59sb.Append(System.Environment.NewLine);

60}

61

62sb.Append("</table>");

63

64

65//----------------------------------------------------------------------

66

67for(intiLoop=0;iLoop<areaList.Count;iLoop++)

68{

69Rectanglerect=(Rectangle)areaList[iLoop];

70stringfileName=this._fileDirectory+""+this._fileNameWithoutExtension+"_"+iLoop.ToString()+this._fileExtension;

71BitmapnewBmp=newBitmap(rect.Width,rect.Height,PixelFormat.Format24bppRgb);

72GraphicsnewBmpGraphics=Graphics.FromImage(newBmp);

73newBmpGraphics.DrawImage(inputImg,newRectangle(0,0,rect.Width,rect.Height),rect,GraphicsUnit.Pixel);

74newBmpGraphics.Save();

75switch(this._fileExtension.ToLower())

76{

77case".jpg":

78case".jpeg":

79newBmp.Save(fileName,ImageFormat.Jpeg);

80break;

81case"gif":

82newBmp.Save(fileName,ImageFormat.Gif);

83break;

84}

85

86}

87inputImg.Dispose();

88stringhtml=sb.ToString();

89returnhtml;

90}

91

92}

【C#实现图片分割方法与代码】相关文章:

c#实现sunday算法实例

C#给picturebox控件加图片选中状态的2个方法

C# 中文简体转繁体实现代码

c#给图片添加文字的代码小结

C#编程实现Excel文档中搜索文本内容的方法及思路

C#读取XML中元素和属性值的实现代码

C#计算代码执行时间的方法

C#.NET字符串比较中忽略符号的方法

C# TrieTree介绍及实现方法

C# 中如何利用lambda实现委托事件的挂接

精品推荐
分类导航