- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Imaging;
- namespace inline
- {
- class makezoomify
- {
- static public bool makeit(string imgpath) {
- //判断文件是否存在
- if (!File.Exists(imgpath)) { return false; }
- string subname = "TileGroup";
- int cropWidth=256;
- int cropHeight=256;
- int picnum = 0;
- string _fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(imgpath);
- string _fileExtension = System.IO.Path.GetExtension(imgpath);
- string _fileDirectory = System.IO.Path.GetDirectoryName(imgpath);
- string filepath = _fileDirectory;
- string filename = _fileNameWithoutExtension;
- //判断图片文件大小,确定循环次数
- Image inputImg = Image.FromFile(imgpath);
- int imgwidth = inputImg.Width;
- int imgheight = inputImg.Height;
- int z = (int)Math.Max(Math.Log((imgwidth), 2.0) -6, Math.Log((imgheight), 2.0)-6);
- //建立输出文件夹
- Directory.CreateDirectory(filepath + "/" + filename);
- //循环输出图片
- for (int i = 0; i < z; i++) {
- double scale = Math.Floor( Math.Pow(2, (z - i-1)));
- int widthCount = (int)Math.Ceiling((imgwidth / scale * 1.00) / (cropWidth * 1.00));
- int heightCount = (int)Math.Ceiling((imgheight / scale * 1.00) / (cropHeight * 1.00));
- //缩放图片
- Image simg = new Bitmap(inputImg, (int)(imgwidth / scale), (int)(imgheight / scale));
- int imgWidth = simg.Width;
- int imgHeight = simg.Height;
- //循环输出
- for (int j = 0; j < widthCount; j++)
- {
- for (int k = 0; k < heightCount; k++)
- {
- //计算绘图位置
- int pointX = j * cropWidth;
- int pointY = k * cropHeight;
- int areaWidth = ((pointX + cropWidth) > imgWidth) ? (imgWidth - pointX) : cropWidth;
- int areaHeight = ((pointY + cropHeight) > imgHeight) ? (imgHeight - pointY) : cropHeight;
- string s = string.Format("{0};{1};{2};{3}", pointX, pointY, areaWidth, areaHeight);
- Rectangle rect = new Rectangle(pointX, pointY, areaWidth, areaHeight);
- //进行绘图并保存
- Bitmap newBmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
- Graphics newBmpGraphics = Graphics.FromImage(newBmp);
- newBmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0, rect.Width, rect.Height));
- newBmpGraphics.DrawImage(simg, new Rectangle(0, 0, rect.Width, rect.Height), rect, GraphicsUnit.Pixel);
- newBmpGraphics.Save();
- //计算保存路径
- int dnum=picnum/256;
- string outdir = filepath + "/" + filename + "/" + subname + dnum;
- if (!Directory.Exists(outdir)) { Directory.CreateDirectory(outdir); }
- string outfilename = outdir + "/" + i + "-" + j + "-" + k + ".jpg";
- newBmp.Save(outfilename, ImageFormat.Jpeg);
- picnum++;
- }
- }
- simg.Dispose();
- }
- //关闭图片并销毁对象
- inputImg.Dispose();
- //输出说明文件
- string outstr = "";
- string cfgfilepath = filepath + "/" + filename + "/ImageProperties.xml";
- StreamWriter sw = new StreamWriter(cfgfilepath);
- sw.WriteLine("<IMAGE_PROPERTIES WIDTH=\\"" + imgwidth + "\\" HEIGHT=\\"" + imgheight + "\\" NUMTILES=\\"" + picnum + "\\" NUMIMAGES=\\"1\\" VERSION=\\"1.8\\" TILESIZE=\\"256\\" />");
- sw.Close();
- return true;
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0805201512517.html
来源: http://www.codesnippet.cn/detail/0805201512517.html