1: private void CreateTiles(string imagePath, string tileDirectoryPath, int tileWidth, int tileHeight)
2: {
3: if (!Directory.Exists(tileDirectoryPath))
4: {
5: Directory.CreateDirectory(tileDirectoryPath);
6: }
7:
8: using (var imageFileStream = new FileStream(imagePath, FileMode.Open))
9: {
10: using (Image originalImage = Image.FromStream(imageFileStream))
11: {
12: double tileLevel = 0;
13: double scaleRate = originalImage.Width/1;
14:
15: while (scaleRate > 1)
16: {
17: double originalWidth = originalImage.Width;
18: double size = Math.Pow(2, tileLevel);
19: scaleRate = (originalWidth/size) > 1 ? originalWidth/size : 1;
20:
21: string tileLevelDirectoryPath = string.Format("{0}/{1}", tileDirectoryPath, tileLevel);
22: if (!Directory.Exists(tileLevelDirectoryPath))
23: {
24: Directory.CreateDirectory(tileLevelDirectoryPath);
25: }
26:
27: using (Image scaledImage = ScaleImage(scaleRate, originalImage))
28: {
29: int tilePositionX = 0;
30: for (int i = 0; i < scaledImage.Width; i += tileWidth)
31: {
32: int tilePositionY = 0;
33: for (int j = 0; j < scaledImage.Height; j += tileHeight)
34: {
35: using (Bitmap tileImage = GetTileImage(i, j, tileWidth, tileHeight,
36: scaledImage))
37: {
38: ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
39: var encoderParameters = new EncoderParameters(1);
40: encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
41: string tilePath = string.Format("{0}/{1}_{2}.jpg", tileLevelDirectoryPath,
42: tilePositionX, tilePositionY);
43:
44: using (var tileFileStream = new FileStream(tilePath, FileMode.OpenOrCreate))
45: {
46: tileImage.Save(tileFileStream, info[1], encoderParameters);
47: }
48: }
49:
50: tilePositionY++;
51: }
52:
53: tilePositionX++;
54: }
55:
56: tileLevel++;
57: }
58: }
59: }
60: }
61: }
62:
63: private static Bitmap GetTileImage(
64: int tileLeft, int tileTop, int tileWidth, int tileHeight, Image scaledImage)
65: {
66: var srcRectnagle = new Rectangle(
67: tileLeft,
68: tileTop,
69: tileLeft + tileWidth < scaledImage.Width
70: ? tileWidth
71: : scaledImage.Width - tileLeft,
72: tileTop + tileHeight < scaledImage.Height
73: ? tileHeight
74: : scaledImage.Height - tileTop);
75:
76: var destRectangle = new Rectangle(0, 0, srcRectnagle.Width, srcRectnagle.Height);
77:
78: var tileImage = new Bitmap(destRectangle.Right, destRectangle.Bottom);
79: using (Graphics graphic = Graphics.FromImage(tileImage))
80: {
81: graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
82: graphic.SmoothingMode = SmoothingMode.HighQuality;
83: graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
84: graphic.CompositingQuality = CompositingQuality.HighQuality;
85:
86: graphic.DrawImage(scaledImage, destRectangle, srcRectnagle, GraphicsUnit.Pixel);
87: }
88:
89: return tileImage;
90: }
91:
92: private static Image ScaleImage(double scaleRate, Image originalImage)
93: {
94: var scaleRect = new Rectangle(
95: 0,
96: 0,
97: Convert.ToInt32(originalImage.Width/scaleRate),
98: Convert.ToInt32(originalImage.Height/scaleRate));
99:
100: Image scaledImage = new Bitmap(scaleRect.Right, scaleRect.Bottom);
101: using (Graphics graphic = Graphics.FromImage(scaledImage))
102: {
103: graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
104: graphic.SmoothingMode = SmoothingMode.HighQuality;
105: graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
106: graphic.CompositingQuality = CompositingQuality.HighQuality;
107:
108: graphic.DrawImage(
109: originalImage,
110: scaleRect,
111: new Rectangle(0, 0, originalImage.Width, originalImage.Height),
112: GraphicsUnit.Pixel);
113: }
114:
115: return scaledImage;
116: }