C#实现图片分割方法与代码

 更新时间:2007年03月12日 00:00:00   作者:  
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    }

相关文章

  • C#使用泛型实现删除数组中重复元素

    C#使用泛型实现删除数组中重复元素

    这篇文章主要为大家详细介绍了C#如何使用泛型实现删除数组中重复元素,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-02-02
  • c# delegate和event的使用说明

    c# delegate和event的使用说明

    这篇文章主要介绍了c# delegate和event的使用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • C#如何使用DateTime.Now.AddDays方法获取任一天的信息

    C#如何使用DateTime.Now.AddDays方法获取任一天的信息

    使用DateTime.Now属性可以得到当前的日期信息,此时调用ToString方法,并在该方法中添加指定的格式化字符串,可以按照要求输出当前日期的信息,本文介绍C#使用DateTime.Now.AddDays方法获取任一天的信息,感兴趣的朋友一起看看吧
    2024-01-01
  • DevExpress之ChartControl实现时间轴实例

    DevExpress之ChartControl实现时间轴实例

    这篇文章主要介绍了DevExpress中ChartControl实现时间轴的方法,涉及相关C#绘图程序用法,具有一定的实用价值,需要的朋友可以参考下
    2014-10-10
  • RabbitMQ的配置与安装教程全纪录

    RabbitMQ的配置与安装教程全纪录

    这篇文章主要给大家介绍了关于RabbitMQ的配置与安装的相关资料,文中通过示例代码以及图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07
  • C#基于WebBrowser获取cookie的实现方法

    C#基于WebBrowser获取cookie的实现方法

    这篇文章主要介绍了C#基于WebBrowser获取cookie的实现方法,实例分析了C#基于WebBrowser简单读取浏览谷歌网站cookie的相关技巧,非常简单实用,需要的朋友可以参考下
    2015-11-11
  • WinForm防止程序重复运行的方法分析

    WinForm防止程序重复运行的方法分析

    这篇文章主要介绍了WinForm防止程序重复运行的方法,通过记录窗口句柄实现防止WinForm程序重复运行的功能,需要的朋友可以参考下
    2017-05-05
  • c# 9.0新特性nint和Pattern matching的使用方法

    c# 9.0新特性nint和Pattern matching的使用方法

    这篇文章主要介绍了c# 9.0新特性nint和Pattern matching的使用方法,文中讲解非常细致,帮助你更好的学习c# 9.0,有需求的朋友可以参考下
    2020-06-06
  • C#.NET 图片水印添加代码

    C#.NET 图片水印添加代码

    这篇文章主要为大家详细介绍了C#.NET 图片水印添加代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Unity利用材质自发光实现物体闪烁

    Unity利用材质自发光实现物体闪烁

    这篇文章主要为大家详细介绍了Unity利用材质自发光实现物体闪烁,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04

最新评论