public bool TileImages() { //this method performs the work of tiling all the images and return true if it succeeds. List<ImageTileInfo> imageTileInfoList = new List<ImageTileInfo>(); string[] files; //get all files in directory and add them to imageTileInfoList if images. try { files = Directory.GetFiles(TargetImagesDirectory); } catch { return false; } Parallel.ForEach(files, file => { try { ImageTileInfo imageTileInfo = new ImageTileInfo(file); imageTileInfoList.Add(imageTileInfo); Bitmap originalBitmap = new Bitmap(imageTileInfo.OriginalFilePath); string path = OutputImagesDirectory + "\\" + imageTileInfo.originalFileInfo.Name + ".bmp"; imageTileInfo.ResizeImage(originalBitmap, TILE_SIZE).Save(path); imageTileInfo.OutputFilePath = path; originalBitmap.Dispose(); } catch { } }); int imageCount = imageTileInfoList.Count; //Sort by name for now imageTileInfoList = imageTileInfoList.OrderBy(x => x.originalFileInfo.Name).ToList(); //Now combine all images into one. int columnCount = 0; int rowCount = 0; if (imageCount > 0) { columnCount = (int)(Math.Sqrt((double)imageCount)) + 1; int remainder = columnCount * columnCount % imageCount; if (remainder > columnCount) rowCount = columnCount - 1; else rowCount = columnCount; } else { return false; } Bitmap combinedImage = new Bitmap(columnCount * TILE_SIZE, rowCount * TILE_SIZE); Graphics gfx = Graphics.FromImage(combinedImage); int imageCounter = 0; //Combine images column by column, row by row for (int row = 0; row < rowCount; row++) { for (int column = 0; column < columnCount; column++) { if (imageCounter < imageCount) gfx.DrawImage(new Bitmap(imageTileInfoList[imageCounter].OutputFilePath), new Point(column * TILE_SIZE, row * TILE_SIZE)); //Save a file at each step of the combination if (developmentMode) combinedImage.Save(OutputImagesDirectory + "\\" + imageCounter + "Combined.bmp"); imageCounter++; } } combinedImage.Save(OutputImagesDirectory + "\\" + "Combined Image " + imageCount + ".bmp"); //If we have gotten this far, everything worked as it should have return true; }
public bool TileImages() { //this method performs the work of tiling all the images and return true if it succeeds. List<ImageTileInfo> imageTileInfoList = new List<ImageTileInfo>(); string[] files; //get all files in directory and add them to OriginalImageList if images. try { files = Directory.GetFiles(TargetImagesDirectory); } catch { return false; } foreach (string file in files) // possible candidate for task parallel library { ImageTileInfo imageTileInfo = new ImageTileInfo(file); if (imageTileInfo.ReadImageFile()) { imageTileInfoList.Add(imageTileInfo); imageTileInfo.ResizeImage(TILE_SIZE); imageTileInfo.OriginalBitmap = null; // remove reference to potentially large Bitmap object that is no longer needed. } } //dump resized images to files int imageCount = 0; foreach (ImageTileInfo iti in imageTileInfoList) { string path = OutputImagesDirectory + "\\" + imageCount.ToString() + ".bmp"; iti.ResizedBitmap.Save(path); iti.OutputFilePath = path; imageCount++; // working, save for testing purposes //iti.ResizedBitmap.Save(OutputImagesDirectory + imageCount.ToString() + ".bmp"); //imageCount++; } imageTileInfoList = imageTileInfoList.OrderBy(x => x.originalFileInfo.Name).ToList(); //now combine all images into one. int columnCount = 0; int rowCount = 0; if(imageCount > 0) { columnCount = (int)(Math.Sqrt((double)imageCount)) + 1; int remainder = columnCount * columnCount % imageCount; if (remainder > columnCount) rowCount = columnCount - 1; else rowCount = columnCount; } else { return false; } Bitmap combinedImage = new Bitmap(columnCount * TILE_SIZE, rowCount * TILE_SIZE); Graphics gfx = Graphics.FromImage(combinedImage); int imageCounter = 0; //combine images row by row for(int row = 0; row < rowCount; row++) { for( int column = 0; column < columnCount; column++) { if (imageCounter < imageCount) gfx.DrawImage(new Bitmap(imageTileInfoList[imageCounter].OutputFilePath), new Point(column * TILE_SIZE, row * TILE_SIZE)); //save a file at each step of the combination if(developmentMode) combinedImage.Save(OutputImagesDirectory + "\\" + imageCounter + "Combined.bmp"); imageCounter++; } } combinedImage.Save(OutputImagesDirectory + "\\" + "Combined Image " + imageCount + ".bmp"); //If we have gotten this far, everything worked as it should have return true; }