public bool Serve(HttpContext context, ImageProviderBase imageProvider, bool cacheToDisk) { if (imageProvider == null) return false; using (MemoryStream memoryStream = new MemoryStream()) { using (DziTile tile = new DziTile(AllTileDefaults.DziTileSize)) { tile.Draw(imageProvider, Level, X, Y); tile.Save(memoryStream, ImageFormat.Jpeg); memoryStream.Position = 0; // Rewind the stream, ready for reading memoryStream.WriteTo(context.Response.OutputStream); context.Response.ContentType = "image/jpeg"; if (cacheToDisk) CacheFile.Save(memoryStream, CachePath); } } return true; }
private bool UpdateLevel(int imageID, int level, int maxLevel, ImageProviderBase imageProvider) { ImageFormat fileFormat = (_fileFormat == "png" ? ImageFormat.Png : ImageFormat.Jpeg); int subTileCount = MortonHelpers.LevelToSubTileCount(level, maxLevel); try { if (_buildCollectionTiles) { int tileX, tileY, subTileX, subTileY; MortonHelpers.MortonBreakdown(imageID, level, maxLevel, out tileX, out tileY, out subTileX, out subTileY); int subTileSize = _tileSize / subTileCount; string tilePath = GetTilePath(_fileFormat, level, tileX, tileY); if (_missingTilesOnly && File.Exists(tilePath)) return false; ImageProvider.FolderPrep(tilePath); using (FileStream fileStream = new FileStream(tilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { using (CollectionTile tile = new CollectionTile(_tileSize, subTileSize)) { if (fileStream.Length > 0) { tile.Load(fileStream); fileStream.Position = 0; } tile.Draw(imageProvider, subTileX * subTileSize, subTileY * subTileSize); tile.Save(fileStream, fileFormat); string publishedTilePath = GetPublishPathFromMasterPath(tilePath); if (File.Exists(publishedTilePath)) File.Delete(publishedTilePath); } } return true; } else { int dimension = subTileCount * _tileSize; int numTilesX = (imageProvider.Size.Width - 1) / dimension; int numTilesY = (imageProvider.Size.Height - 1) / dimension; bool ok = false; for (int tileX = 0; tileX <= numTilesX; tileX++) { for (int tileY = 0; tileY <= numTilesY; tileY++) { string tilePath = GetTilePath(_fileFormat, level, tileX, tileY); if (_missingTilesOnly && File.Exists(tilePath)) continue; ImageProvider.FolderPrep(tilePath); using (FileStream fileStream = new FileStream(tilePath, FileMode.Create, FileAccess.Write)) { using (DziTile tile = new DziTile(_tileSize)) { tile.Draw(imageProvider, level, tileX, tileY); tile.Save(fileStream, fileFormat); } } ok |= true; } } return ok; } } catch (Exception ex) { ex.GetType(); } return false; }