using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Drawing; using System.Collections; using System.Net; namespace tileserver { class ImageFactory { public static byte[] getImageData(int x1, int y1, int x2, int y2, int zoom, String[] layers, int width, int height, String path) { int cols = (x2 - x1 + 1); int rows = (y2 - y1 + 1); Tiles tiles = new Tiles(); { tiles.Zoom = zoom; tiles.Width = Program.TILE_SIZE; tiles.Height = Program.TILE_SIZE; tiles.Zoom = zoom; tiles.Layers = layers; tiles.ListTiles = new Tile[cols * rows]; int apuntador = 0; for (int i = x1; i <= x2; i++) { for (int j = y1; j <= y2; j++) { Tile tile = new Tile(); tile.X = i; tile.Y = j; tiles.ListTiles[apuntador] = tile; apuntador++; } } } MetaReader.fillTiles(Program.folder + "/" + path + "/", tiles); return merge(tiles, width, height, cols, rows); } public static byte[] merge(Tiles tiles, int width, int height, int cols, int rows) { byte[] result = new byte[0]; Bitmap bitmap = null; Graphics graphics = null; try { bitmap = new Bitmap(width, height); graphics = Graphics.FromImage(bitmap); graphics.Clear(Color.FromArgb(255, 170, 211, 223)); int apuntador = 0; for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { byte[] data = tiles.ListTiles[apuntador].Data; MemoryStream memoryStream = new MemoryStream(data, 0, data.Length); memoryStream.Write(data, 0, data.Length); Image imagen = Image.FromStream(memoryStream, true); if (imagen != null) { graphics.DrawImageUnscaled(imagen, new Point(i * Program.TILE_SIZE, j * Program.TILE_SIZE)); imagen.Dispose(); } apuntador++; } } using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Close(); result = stream.ToArray(); } } catch (Exception) { } try { bitmap.Dispose(); } catch (Exception) { }; try { graphics.Dispose(); } catch (Exception) { }; return result; } public static void merge(Tiles tiles, Tile tile, List ldata) { Bitmap imageMerge = null; Graphics graphics = null; try { imageMerge = new Bitmap(tiles.Width, tiles.Height); graphics = Graphics.FromImage(imageMerge); graphics.Clear(Color.FromArgb(255, 170, 211, 223)); for (int i = 0; i < ldata.Count; i++) { Image imagen = null; try { byte[] data = ldata.ElementAt(i); MemoryStream memoryStream = new MemoryStream(data, 0, data.Length); memoryStream.Write(data, 0, data.Length); imagen = Image.FromStream(memoryStream, true); graphics.DrawImageUnscaled(imagen, new Point(0, 0)); imagen.Dispose(); } catch (Exception) { } try { imagen.Dispose(); } catch (Exception) { }; } //String coordinates = tile.X + " " + tile.Y; //graphics.DrawString(coordinates, new Font(FontFamily.GenericSansSerif, 15, FontStyle.Regular), new SolidBrush(Color.Black), 15, 15); //graphics.Dispose(); using (MemoryStream stream = new MemoryStream()) { imageMerge.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Close(); tile.Data = stream.ToArray(); } imageMerge.Dispose(); } catch (Exception) { } try { graphics.Dispose(); } catch (Exception) { }; try { imageMerge.Dispose(); } catch (Exception) { }; } public static byte[] crop(byte[] data, int x, int y, int width, int height) { byte[] result = new byte[0]; Image imageSource = null; Bitmap imageDestination = null; Graphics graphics = null; try { MemoryStream memoryStream = new MemoryStream(data, 0, data.Length); memoryStream.Write(data, 0, data.Length); imageSource = Image.FromStream(memoryStream, true); imageDestination = new Bitmap(width, height); graphics = Graphics.FromImage(imageDestination); graphics.Clear(Color.FromArgb(255, 170, 211, 223)); graphics.DrawImageUnscaled(imageSource, new Point(-x, -y)); } catch (Exception) { } using (MemoryStream stream = new MemoryStream()) { imageDestination.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Close(); result = stream.ToArray(); } try { graphics.Dispose(); } catch (Exception) { }; try { imageSource.Dispose(); } catch (Exception) { }; try { imageDestination.Dispose(); } catch (Exception) { }; return result; } } }