C# GDI+ 实现图片分隔

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 对象的指定部分。

图片分隔说明:就是把一张大图,按指定的宽度和高度分隔成一组小图

图1

对初学者的提示:在我们读书时学过的数学坐标如图2所示,在GDI+里的坐标如图3所示

图2图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    }
 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/408005.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

让人吐血的文章,要被气死了

来源&#xff1a;[url]http://dx.3800hk.com/news/w21/124454.html[/url]请注意文中这两部分新世纪&#xff1a;为什么在李俊落网之前&#xff0c;很多人都怀疑这是杀毒软件公司的人干的&#xff0c;目的是为了多卖几套杀毒软件&#xff1f;李铁军&#xff1a;人们一直都有这种…

共享锁和排它锁---C++17 多线程

共享锁和排它锁—C17 多线程 读写锁把对共享资源的访问者划分成读者和写者&#xff0c;读者只对共享资源进行读访问&#xff0c;写者则需要对共享资源进行写操作。C17开始&#xff0c;标准库提供了shared_mutex类&#xff08;在这之前&#xff0c;可以使用boost的shared_mutex…

将人民币的数字表示转化成大写表示(C#版)

using System; namespace Test.Com{ /// <summary> /// 功能&#xff1a;字符串处理函数集 /// </summary> public class DealString { #region 私有成员 /// <summary> /// 输入字符串 /// </summary> private string inputStringnull; /// <…

C#图片切割

图片切割就是把一幅大图片按用户要求切割成多幅小图片。dotnet环境下系统提供了GDI类库&#xff0c;为图像操作处理提供了方便的接口。 下面是图像切割小程序&#xff1a; public class ImageManager { /// <summary> /// 图像切割 /// </s…

条件变量 ---C++17 多线程

条件变量 —C17 多线程 C标准库提供了条件变量的两种实现&#xff1a;std::condition_variable 和std::condition_variable_any。它们都在标准库的头文件<condition_variable>内声明。两者都需配合互斥&#xff0c;方能提供妥当的同步操作。std::condition_variable仅限…

关于在asp.net中textbox文本输入框中的汉语标点符号显示位置的问题

在asp.net中的服务器控件textbox中输入中文标点符号&#xff0c;位置处于输入框中间&#xff0c;而不是靠在左下角&#xff0c;解决办法&#xff1a;把字体样式设置为其它&#xff0c;比如&#xff1a;微软雅黑。这个问题&#xff0c;仅在宋体的时候出现过。 转载于:https://ww…

考验

如果不做网站&#xff0c;可以做着不错的工作&#xff0c;过着安逸的生活&#xff0c;可是&#xff0c;我不想年老的时候后悔&#xff1a;这一生竟然没有为自己的理想拼搏过!仅这一个理由&#xff0c;足以让我坚强地面对任何考验!博客园的发展需要付出更多努力&#xff0c;开始…

汇编常用命令、指令一览

MOV&#xff08;MOVe&#xff09; 传送指令P28 PUSH 入栈指令P32 POP 出栈指令P33 XCHG&#xff08;eXCHanG&#xff09; 交换指令P34 XLAT&#xff08;TRANSLATE&#xff09; 换码指令P34 LEA &#xff08;Load Effective Address&#xff09; 有效地址送…

std::future ---C++17 多线程

std::future —C17 多线程 std::future C标准程序库使用future来模拟这类一次性事件&#xff1a;若线程需等待某个特定的一次性事件发生&#xff0c;则会以恰当的方式取得一个future&#xff0c;它代表目标事件&#xff1b;接着&#xff0c;该线程就能一边执行其他任务&#…

VNCserver在Fedora上配置过程

前言&#xff1a;一直想写一下vncserver在redhat下详细配置过程&#xff0c;以帮助一些向我有同样需求却有懒得去读man page的朋友&#xff0c;后来在www.fedoranews.org上发现已经有人写了一个教程&#xff0c;并且还不错。干脆翻译算了。大家可以直接去阅原文&#xff0c;我这…

学好英语的42个经典要诀(完整版)

第一要诀&#xff1a;收听英语气象报告 有些教学录音带为配合初学者的学习&#xff0c;故意放慢语速&#xff0c;这对英语听力的训练是不够的。如果听语速正常的英语&#xff0c;初学者又会感到力不从心。英语气象报告的速度虽快&#xff0c;但词汇简单固定&#xff0c;内容单纯…

std::packaged_task() ---C++17 并发编程

std::packaged_task() —C17 并发编程 std::packaged_task<>连结了future对象与函数&#xff08;或可调用对象&#xff09;。 std::packaged_task<>对象在执行任务时&#xff0c;会调用关联的函数&#xff08;或可调用对象&#xff09;&#xff0c;把返回值保存为…

js分页--存储数据并进行分页

//分页方法var page function(){this.v {o:null,//ul父级层home:null,previous:null,next:null,last:null, list:[],pageSize:10,pageIndex:0,pageCount:0,rowCount:0};this.init function(){var _this this;_this.v.o.find("li").each(function(i,o){_this.v.…

c/c++面试试题(一)

1.求下面函数的返回值&#xff08;微软&#xff09;int func(x) { int countx 0; while(x) { countx ; x x&(x-1); } return countx; } 假定x 9999。 答案&#xff1a;8思路&#xff1a;将x转化为2进制&#xff0c;看含有的1…

react(78)--vs打开setting.json

1.ctrl shift p 2.输入setting 3.找到这一项

stdspan ---C++20

std::span —C20 std::span的定义 template<class T,std::size_t Extent std::dynamic_extent > class span;std::span是指向一组连续的对象的对象, 是一个视图view, 不是一个拥有者owner 一组连续的对象可以是 C 数组, 带着大小的指针, std::array, 或者 std::strin…