private void OnMapFilled(object sender, ThumbnailMapCollectionEventArgs e) { ThumbnailMapCollection mapCollection = sender as ThumbnailMapCollection; ZipEntry oZipEntry = new ZipEntry("ThumbnailMap" + mapCollection.Maps.Count + ".bmp"); this.oZipStream.PutNextEntry(oZipEntry); int size = mapCollection.MapSizeInBytes; byte[] buffer = new byte[size]; MemoryStream ms = new MemoryStream(buffer); e.Map.Save(ms, FREE_IMAGE_FORMAT.FIF_BMP); oZipStream.Write(buffer, 0, size); e.Map.Dispose(); ms.Dispose(); }
internal override void ReadThumbnails(TileLoadInfo info) { this.ThreadController.ReportThreadStarted(this, "Loading files"); ZipEntry zipEntry; int totalCount = 0; int count = 0; ThumbnailMapCollection mapCollection = new ThumbnailMapCollection(this.thumbnailWidth, this.thumbnailHeight); this.s.Close(); this.s = new ZipInputStream(File.OpenRead(this.FilePath)); totalCount = 0; try { // First go through the zip file and get the image maps while ((zipEntry = this.s.GetNextEntry()) != null) { if (zipEntry.Name.StartsWith("Thumbnail")) { if (this.ThreadController.ThreadAborted) return; count = 0; ThumbnailMap map = new ThumbnailMap(this.thumbnailWidth, this.thumbnailHeight, this.s); Tile tile = null; for (int i = 0; i < map.MapSizeInTiles && totalCount < MosaicWindow.MosaicInfo.Items.Count; i++) { tile = MosaicWindow.MosaicInfo.Items[totalCount]; FreeImageAlgorithmsBitmap fib = map.GetImage(count); tile.Thumbnail = fib; if (tile.Thumbnail == null) { throw new Exception("Failed to retrive tumbnail from map"); } this.ThreadController.ReportThreadPercentage(this, "Creating Thumbnails", totalCount++, MosaicWindow.MosaicInfo.Items.Count); count++; } map.Dispose(); } } } catch (Exception e) { MessageBox.Show(e.Message); FailedToReadFormat = true; this.ThreadController.ReportThreadCompleted(this, "Loaded file", false); return; } this.s.Close(); this.sr.Close(); info.FreeImageType = MosaicWindow.MosaicInfo.Items[0].FreeImageType; info.ColorDepth = MosaicWindow.MosaicInfo.Items[0].ColorDepth; this.ThreadController.ReportThreadCompleted(this, "Loaded file", false); }
// Save mosaic data. internal void SaveMosaicCacheFile() { if (this.threadController != null) this.threadController.ReportThreadStarted(this, "Saving Tiles"); string tempFilePath = Path.GetTempFileName(); try { this.oZipStream = new ZipOutputStream(System.IO.File.Create(tempFilePath)); } catch(Exception e) { MessageBox.Show(e.Message); return; } this.oZipStream.SetLevel(2); // 9 = maximum compression level ZipEntry oZipEntry = new ZipEntry("info.txt"); this.oZipStream.PutNextEntry(oZipEntry); StreamWriter sw = new StreamWriter(oZipStream); TileSaver.SaveMosaicHeader(MosaicWindow.MosaicInfo, sw); ThumbnailMapCollection mapCollection = new ThumbnailMapCollection(MosaicWindow.MosaicInfo.Items[0].Thumbnail.Width, MosaicWindow.MosaicInfo.Items[0].Thumbnail.Height); mapCollection.MapFilled += new ThumbnailMapCollectionEventHandler(OnMapFilled); int count = 1; int requiredMaps = (MosaicWindow.MosaicInfo.Items.Count / ThumbnailMap.MaxSize) + 1; foreach (Tile tile in MosaicWindow.MosaicInfo.Items) // For each file, generate a zipentry { if (this.threadController.ThreadAborted) return; mapCollection.AddThumbnail(tile); if (this.threadController != null) threadController.ReportThreadPercentage(this, "Creating Tile Maps", count++, MosaicWindow.MosaicInfo.Items.Count); } // Final map may not get completly filled // if not then save it here if(mapCollection.Maps.Count <= requiredMaps) { ThumbnailMapCollectionEventArgs thumbnailEventArgs = new ThumbnailMapCollectionEventArgs(mapCollection.Last); OnMapFilled(mapCollection, thumbnailEventArgs); } mapCollection.Dispose(); oZipStream.Finish(); oZipStream.Dispose(); oZipStream.Close(); try { File.Delete(MosaicWindow.MosaicInfo.TileReader.GetCacheFilePath()); File.Copy(tempFilePath, MosaicWindow.MosaicInfo.TileReader.GetCacheFilePath()); } catch (Exception) { } //File.SetAttributes(this.filePath, FileAttributes.Hidden); if (this.threadController != null) this.threadController.ReportThreadCompleted(this, "Cache Saved", false); GC.Collect(); }
internal override void ReadThumbnails(TileLoadInfo info) { this.ThreadController.ReportThreadStarted(this, "Loading files"); ZipEntry zipEntry; int totalCount = 0; int count = 0; ThumbnailMapCollection mapCollection = new ThumbnailMapCollection(this.thumbnailWidth, this.thumbnailHeight); this.s.Close(); this.s = new ZipInputStream(File.OpenRead(this.FilePath)); totalCount = 0; try { // First go through the zip file and get the image maps while ((zipEntry = this.s.GetNextEntry()) != null) { if (zipEntry.Name.StartsWith("Thumbnail")) { if (this.ThreadController.ThreadAborted) { return; } count = 0; ThumbnailMap map = new ThumbnailMap(this.thumbnailWidth, this.thumbnailHeight, this.s); Tile tile = null; for (int i = 0; i < map.MapSizeInTiles && totalCount < MosaicWindow.MosaicInfo.Items.Count; i++) { tile = MosaicWindow.MosaicInfo.Items[totalCount]; FreeImageAlgorithmsBitmap fib = map.GetImage(count); tile.Thumbnail = fib; if (tile.Thumbnail == null) { throw new Exception("Failed to retrive tumbnail from map"); } this.ThreadController.ReportThreadPercentage(this, "Creating Thumbnails", totalCount++, MosaicWindow.MosaicInfo.Items.Count); count++; } map.Dispose(); } } } catch (Exception e) { MessageBox.Show(e.Message); FailedToReadFormat = true; this.ThreadController.ReportThreadCompleted(this, "Loaded file", false); return; } this.s.Close(); this.sr.Close(); info.FreeImageType = MosaicWindow.MosaicInfo.Items[0].FreeImageType; info.ColorDepth = MosaicWindow.MosaicInfo.Items[0].ColorDepth; this.ThreadController.ReportThreadCompleted(this, "Loaded file", false); }
// Save mosaic data. internal void SaveMosaicCacheFile() { if (this.threadController != null) { this.threadController.ReportThreadStarted(this, "Saving Tiles"); } string tempFilePath = Path.GetTempFileName(); try { this.oZipStream = new ZipOutputStream(System.IO.File.Create(tempFilePath)); } catch (Exception e) { MessageBox.Show(e.Message); return; } this.oZipStream.SetLevel(2); // 9 = maximum compression level ZipEntry oZipEntry = new ZipEntry("info.txt"); this.oZipStream.PutNextEntry(oZipEntry); StreamWriter sw = new StreamWriter(oZipStream); TileSaver.SaveMosaicHeader(MosaicWindow.MosaicInfo, sw); ThumbnailMapCollection mapCollection = new ThumbnailMapCollection(MosaicWindow.MosaicInfo.Items[0].Thumbnail.Width, MosaicWindow.MosaicInfo.Items[0].Thumbnail.Height); mapCollection.MapFilled += new ThumbnailMapCollectionEventHandler(OnMapFilled); int count = 1; int requiredMaps = (MosaicWindow.MosaicInfo.Items.Count / ThumbnailMap.MaxSize) + 1; foreach (Tile tile in MosaicWindow.MosaicInfo.Items) // For each file, generate a zipentry { if (this.threadController.ThreadAborted) { return; } mapCollection.AddThumbnail(tile); if (this.threadController != null) { threadController.ReportThreadPercentage(this, "Creating Tile Maps", count++, MosaicWindow.MosaicInfo.Items.Count); } } // Final map may not get completly filled // if not then save it here if (mapCollection.Maps.Count <= requiredMaps) { ThumbnailMapCollectionEventArgs thumbnailEventArgs = new ThumbnailMapCollectionEventArgs(mapCollection.Last); OnMapFilled(mapCollection, thumbnailEventArgs); } mapCollection.Dispose(); oZipStream.Finish(); oZipStream.Dispose(); oZipStream.Close(); try { File.Delete(MosaicWindow.MosaicInfo.TileReader.GetCacheFilePath()); File.Copy(tempFilePath, MosaicWindow.MosaicInfo.TileReader.GetCacheFilePath()); } catch (Exception) { } //File.SetAttributes(this.filePath, FileAttributes.Hidden); if (this.threadController != null) { this.threadController.ReportThreadCompleted(this, "Cache Saved", false); } GC.Collect(); }