public DBReaderResult GetImageFromDb(string id, string region, string scaling) { var bookId = tools.GetBookIdFromRequestId(id); var pageId = tools.GetPageIdFromRequestId(id); using (var context = new TilesContext()) { var tiles = context.Tiles.Where(t => t.BookId == bookId && t.PageId == pageId); if (tiles.Any()) { var result = new DBReaderResult() {RegionNew = region, ScalingNew = scaling}; PageTileInfo pti; if (scaling != "256,0" && scaling != "512,0" && !tiles.Any(t=>t.IsScaled)) { var tuple = tools.LookForClosestTile(tiles, region); pti = tuple.Item1; result.RegionNew = tuple.Item2; } else { var parts = scaling.Split(','); var scaleTo = int.Parse(parts[0]); var tuple = tools.LookForClosestTileScaled(tiles, region, scaleTo); pti = tuple.Item1; result.RegionNew = tuple.Item2; result.ScalingNew = tuple.Item3; } var tileContent = context.TilesContent.First(tc => tc.InfoID == pti.ID); result.Content = tileContent; return result; } return null; } }
private async Task <TileCacheManager> CreateTileCacheManager(Guid tileCacheId) { string path = Path.Combine(ServiceOptions.DirectoryRoot, "TileCaches", $"{tileCacheId}.mbtiles"); DbContextOptionsBuilder <TilesContext> dbContextOptionsBuilder = new DbContextOptionsBuilder <TilesContext>(); dbContextOptionsBuilder.UseSqlite($"Filename={path}"); TilesContext tilesContext = new TilesContext(dbContextOptionsBuilder.Options); await tilesContext.Database.EnsureCreatedAsync(); return(new TileCacheManager(tilesContext)); }
public void PutImageToDb(int bookId, int pageId, string path) { using (var image = Image.FromFile(path)) { using (var context = new TilesContext()) { var tiles = tools.GenerateTilesInfo(image.Width,image.Height); var addedTiles = new List<PageTileInfo>(); foreach (PageTileInfo pt in tiles) { pt.BookId = bookId; pt.PageId = pageId; var added = context.Tiles.Add(pt); addedTiles.Add(added); } context.SaveChanges(); foreach (var pt in addedTiles) { Image tileImage = cropProcessor.SizeCrop(image, pt.XOffset, pt.YOffset, pt.Width, pt.Heigth); if (pt.IsScaled) { var scaled = scaleProcessor.SizeScaling((Bitmap)tileImage, pt.DestWidth, 0, false); tileImage = scaled; } using (var memStream = new MemoryStream()) { tileImage.Save(memStream, ImageFormat.Jpeg); var bytes = memStream.ToArray(); var ptc = new PageTileContent() { InfoID = pt.ID, TileContent = bytes }; context.TilesContent.Add(ptc); } } context.SaveChanges(); } } }
public TileCacheManager(TilesContext tilesContext) { TilesContext = tilesContext; }