|
| 1 | +namespace ImageMerger |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Drawing; |
| 6 | + using System.Drawing.Imaging; |
| 7 | + using System.IO; |
| 8 | + using System.Windows.Forms; |
| 9 | + |
| 10 | + public class TileMerger |
| 11 | + { |
| 12 | + private int mergedImageCount = 0; |
| 13 | + |
| 14 | + public static Bitmap MergeBitmaps(List<Bitmap> listOfTileBitmaps, int columnCount, bool horizontalTiling) |
| 15 | + { |
| 16 | + int width = 1; |
| 17 | + int height = 1; |
| 18 | + foreach (Bitmap sourceTile in listOfTileBitmaps) |
| 19 | + { |
| 20 | + if (sourceTile.Width > width) |
| 21 | + { |
| 22 | + width = sourceTile.Width; |
| 23 | + } |
| 24 | + if (sourceTile.Height > height) |
| 25 | + { |
| 26 | + height = sourceTile.Height; |
| 27 | + } |
| 28 | + } |
| 29 | + if (columnCount > listOfTileBitmaps.Count) |
| 30 | + { |
| 31 | + columnCount = listOfTileBitmaps.Count; |
| 32 | + } |
| 33 | + int rowCount = (int)Math.Ceiling((double)(((double)listOfTileBitmaps.Count) / ((double)columnCount))); |
| 34 | + if (!horizontalTiling) |
| 35 | + { |
| 36 | + int temp = rowCount; |
| 37 | + rowCount = columnCount; |
| 38 | + columnCount = temp; |
| 39 | + } |
| 40 | + Bitmap image = new Bitmap(columnCount * width, rowCount * height, PixelFormat.Format32bppArgb); |
| 41 | + Graphics graphics = Graphics.FromImage(image); |
| 42 | + SolidBrush brush = new SolidBrush(Color.FromArgb(0, 0xff, 240, 200)); |
| 43 | + graphics.FillRectangle(brush, 0, 0, image.Width, image.Height); |
| 44 | + int n = 0; |
| 45 | + if (horizontalTiling) |
| 46 | + { |
| 47 | + foreach (Bitmap tileBitmap in listOfTileBitmaps) |
| 48 | + { |
| 49 | + int col = (int)Math.Floor((double)(((double)n) / ((double)columnCount))); |
| 50 | + int row = n % columnCount; |
| 51 | + Point location = new Point(((row * width) + (width / 2)) - (tileBitmap.Width / 2), ((col * height) + (height / 2)) - (tileBitmap.Height / 2)); |
| 52 | + Size size = new Size(tileBitmap.Width, tileBitmap.Height); |
| 53 | + Graphics.FromImage(image).DrawImage(tileBitmap, new Rectangle(location, size)); |
| 54 | + n++; |
| 55 | + } |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + foreach (Bitmap tileBitmap in listOfTileBitmaps) |
| 60 | + { |
| 61 | + int row = (int)Math.Floor((double)(((double)n) / ((double)rowCount))); |
| 62 | + int col = n % rowCount; |
| 63 | + Point location = new Point(((row * width) + (width / 2)) - (tileBitmap.Width / 2), ((col * height) + (height / 2)) - (tileBitmap.Height / 2)); |
| 64 | + Size size = new Size(tileBitmap.Width, tileBitmap.Height); |
| 65 | + Graphics.FromImage(image).DrawImage(tileBitmap, new Rectangle(location, size)); |
| 66 | + n++; |
| 67 | + } |
| 68 | + } |
| 69 | + return image; |
| 70 | + } |
| 71 | + |
| 72 | + public bool ProcessDirectoryToFile(Form parentForm, string directory, string fileTarget, int columnCount, string filter, bool horizontalTiling=true) |
| 73 | + { |
| 74 | + Bitmap bitmap; |
| 75 | + this.mergedImageCount = 0; |
| 76 | + if (!Directory.Exists(directory)) |
| 77 | + { |
| 78 | + MessageBox.Show(parentForm, "The specified source directory " + directory + " does not exist.", "Folder not found error", MessageBoxButtons.OK); |
| 79 | + return false; |
| 80 | + } |
| 81 | + if (fileTarget == "") |
| 82 | + { |
| 83 | + MessageBox.Show(parentForm, "An invalid target file was set.", "Operation stopped", MessageBoxButtons.OK); |
| 84 | + return false; |
| 85 | + } |
| 86 | + List<Bitmap> bitmaps = ImageLoader.LoadImages(ImageLoader.ListFiles(directory, filter)); |
| 87 | + if (bitmaps.Count == 0) |
| 88 | + { |
| 89 | + MessageBox.Show(parentForm, "No images were found, could not create merged image.", "Operation stopped", MessageBoxButtons.OK); |
| 90 | + return false; |
| 91 | + } |
| 92 | + try |
| 93 | + { |
| 94 | + bitmap = MergeBitmaps(bitmaps, columnCount, horizontalTiling); |
| 95 | + } |
| 96 | + catch (Exception exception) |
| 97 | + { |
| 98 | + MessageBox.Show(parentForm, exception.Message, "Merge Bitmaps Exception", MessageBoxButtons.OK); |
| 99 | + ImageLoader.DisposeImages(bitmaps); |
| 100 | + return false; |
| 101 | + } |
| 102 | + try |
| 103 | + { |
| 104 | + SaveImage(bitmap, fileTarget); |
| 105 | + } |
| 106 | + catch (Exception exception2) |
| 107 | + { |
| 108 | + MessageBox.Show(parentForm, exception2.Message, "Save Image Exception", MessageBoxButtons.OK); |
| 109 | + return false; |
| 110 | + } |
| 111 | + this.mergedImageCount = bitmaps.Count; |
| 112 | + ImageLoader.DisposeImages(bitmaps); |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + public static void SaveImage(Bitmap image, string fileTarget) |
| 117 | + { |
| 118 | + image.Save(fileTarget, ImageFormat.Png); |
| 119 | + } |
| 120 | + |
| 121 | + public int MergedImageCount |
| 122 | + { |
| 123 | + get |
| 124 | + { |
| 125 | + return this.mergedImageCount; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
0 commit comments