1- using System ;
2- using System . Collections . Generic ;
3- using System . Drawing ;
4- using System . Drawing . Imaging ;
5- using System . Linq ;
1+ using ImageMagick ;
2+ using System ;
63using System . Windows . Forms ;
74
85namespace TerrariaImageStitcher
96{
107 public partial class Form1 : Form
118 {
12-
139 // Say Hello To Decompilers
14- private readonly string HelloThere = "Hello there fellow Decompiler, This Program Was Made By D.RU$$ (xXCrypticNightXx)." ;
10+ private readonly string HelloThere = "Hello there fellow Decompiler, This Program Was Made By dannyruss (xXCrypticNightXx)." ;
1511
1612 #region Main Code
1713
@@ -26,18 +22,16 @@ public Form1()
2622 public int imgtall = 0 ;
2723
2824 // Stitch Photo Function
29- public System . Drawing . Bitmap CombineBitmap ( string [ ] files )
25+ public void CombineBitmap ( string [ ] files )
3026 {
31- //read all images into memory
32- List < System . Drawing . Bitmap > images = new List < System . Drawing . Bitmap > ( ) ;
33- System . Drawing . Bitmap finalImage = null ;
27+ // Setup Progress Bar
28+ progressBar1 . Step = 1 ;
29+ progressBar1 . PerformStep ( ) ;
3430
3531 try
3632 {
3733 int imgwide = 0 ;
3834 int imgtall = 0 ;
39- int width = 0 ;
40- int height = 0 ;
4135
4236 int widecount = 0 ;
4337 int tallcount = 0 ;
@@ -46,13 +40,17 @@ public System.Drawing.Bitmap CombineBitmap(string[] files)
4640 bool lockit = false ;
4741 bool dead = false ;
4842
49- int tempwidth = 1 ;
50- int temptall = 0 ;
43+ int stackedImagesTall = 0 ;
44+ int stackedImagesWide = 1 ;
5145
5246 int tallcountrunner = 0 ;
5347
5448 foreach ( string image in files )
5549 {
50+ // Add each image from files to an image array.
51+ // images.Add(new MagickImage(image));
52+
53+ // Continue to do math on existing images.
5654 int pos = image . LastIndexOf ( @"\" ) + 1 ;
5755 string filenumber = image . Substring ( pos , image . Length - pos ) . GetUntilOrEmpty ( ) ;
5856
@@ -61,7 +59,7 @@ public System.Drawing.Bitmap CombineBitmap(string[] files)
6159 // Look For First Number
6260 if ( ! dead )
6361 {
64- temptall ++ ;
62+ stackedImagesTall ++ ;
6563 oldvalue = int . Parse ( filenumber ) ;
6664 }
6765 lockit = true ;
@@ -72,51 +70,69 @@ public System.Drawing.Bitmap CombineBitmap(string[] files)
7270 dead = true ;
7371 lockit = false ;
7472 oldvalue = int . Parse ( filenumber ) ;
75- tempwidth ++ ;
73+ stackedImagesWide ++ ;
7674 }
7775 else
7876 {
7977 if ( ! dead )
8078 {
81- temptall ++ ;
79+ stackedImagesTall ++ ;
8280 }
8381 }
8482 }
83+ // Setup Progress Bar.
84+ progressBar1 . Maximum = ( stackedImagesWide * stackedImagesTall ) + 2 ;
8585
86- // Setup Progress Bar
87- progressBar1 . Step = 1 ;
88- progressBar1 . Maximum = ( tempwidth * temptall ) + 1 ;
89-
90- foreach ( string image in files )
86+ // Grab the final images width and height to reduce load time.
87+ foreach ( string file in files )
9188 {
92- //create a Bitmap from the file and add it to the list
93- System . Drawing . Bitmap bitmap = new System . Drawing . Bitmap ( image ) ;
94-
95- //update the size of the final bitmap
96- width += bitmap . Width ;
97- height = bitmap . Height > height ? bitmap . Height : height ;
98-
99- images . Add ( bitmap ) ;
89+ using ( MagickImage image = new MagickImage ( file ) )
90+ {
91+ tallcountrunner ++ ;
92+ if ( imgwide == 0 )
93+ tallcount += image . Height ;
94+ if ( tallcountrunner < stackedImagesTall )
95+ {
96+ imgtall += 2016 ;
97+ }
98+ else
99+ {
100+ tallcountrunner = 0 ;
101+ imgtall = 0 ;
102+ imgwide += 2016 ;
103+ widecount += image . Width ;
104+ }
105+ }
100106 }
101-
102- finalImage = new System . Drawing . Bitmap ( width , ( temptall * 2048 ) + 32 ) ; // Fix 1.2: width, (temptall * 2048) + 32 - Fixed single horizontal issue.
103-
104- //get a graphics object from the image so we can draw on it
105- using ( System . Drawing . Graphics g = System . Drawing . Graphics . FromImage ( finalImage ) )
107+ // Define real image height.
108+ int IWidth = widecount - ( ( stackedImagesWide - 1 ) * 32 ) ;
109+ int IHeight = tallcount - ( ( stackedImagesTall - 1 ) * 32 ) ;
110+
111+ // Reset variables.
112+ widecount = 0 ;
113+ tallcount = 0 ;
114+ imgtall = 0 ;
115+ imgwide = 0 ;
116+ tallcountrunner = 0 ;
117+
118+ // Define a new image using the finals height and width.
119+ MagickImage finalImage = new MagickImage ( MagickColors . White , IWidth , IHeight ) ;
120+
121+ // Go through each image and draw it on the final image
122+ foreach ( string file in files )
106123 {
107- //set background color
108- g . Clear ( System . Drawing . Color . Transparent ) ;
109- g . InterpolationMode = System . Drawing . Drawing2D . InterpolationMode . NearestNeighbor ;
110-
111- //go through each image and draw it on the final image
112- foreach ( System . Drawing . Bitmap image in images )
124+ using ( MagickImage image = new MagickImage ( file ) )
113125 {
126+ // Define image quality.
127+ image . Quality = 100 ;
128+
129+ // Draw new image on top of final.
130+ finalImage . Draw ( new DrawableComposite ( imgwide , imgtall , image ) ) ;
114131
115- g . DrawImage ( image , new System . Drawing . Rectangle ( imgwide , imgtall , image . Width , image . Height ) ) ;
116132 tallcountrunner ++ ;
117133 if ( imgwide == 0 )
118134 tallcount += image . Height ;
119- if ( tallcountrunner < temptall )
135+ if ( tallcountrunner < stackedImagesTall )
120136 {
121137 imgtall += 2016 ;
122138 progressBar1 . PerformStep ( ) ;
@@ -130,34 +146,65 @@ public System.Drawing.Bitmap CombineBitmap(string[] files)
130146 widecount += image . Width ;
131147 }
132148 }
133- tallcount -= ( temptall - 1 ) * 32 ;
134- widecount -= ( tempwidth - 1 ) * 32 ;
135149 }
136150
137- // Crop Image
138- Bitmap finalImagecroped = new System . Drawing . Bitmap ( widecount , tallcount ) ;
139- finalImagecroped = finalImage . Clone ( new Rectangle ( 0 , 0 , widecount , tallcount ) , PixelFormat . DontCare ) ;
151+ // Define settings and export image.
152+ using ( IMagickImage result = finalImage )
153+ {
154+ // Get the image format.
155+ string imageFormat = "" ;
156+ if ( radioButton1 . Checked )
157+ {
158+ result . Format = MagickFormat . Png ;
159+ imageFormat = ".png" ;
160+ }
161+ else if ( radioButton2 . Checked )
162+ {
163+ result . Format = MagickFormat . Jpeg ;
164+ imageFormat = ".jpeg" ;
165+ }
166+ else if ( radioButton3 . Checked )
167+ {
168+ result . Format = MagickFormat . Bmp ;
169+ imageFormat = ".bmp" ;
170+ }
171+ else if ( radioButton4 . Checked )
172+ {
173+ result . Format = MagickFormat . Emf ;
174+ imageFormat = ".emf" ;
175+ }
176+ else if ( radioButton5 . Checked )
177+ {
178+ result . Format = MagickFormat . Icon ;
179+ imageFormat = ".icon" ;
180+ }
181+ else if ( radioButton6 . Checked )
182+ {
183+ result . Format = MagickFormat . Gif ;
184+ imageFormat = ".gif" ;
185+ }
186+ else if ( radioButton6 . Checked )
187+ {
188+ result . Format = MagickFormat . Wmf ;
189+ imageFormat = ".wmf" ;
190+ }
191+
192+ // Define quality.
193+ result . Quality = 100 ;
194+
195+ // Write output file.
196+ result . Write ( textBox2 . Text + imageFormat ) ;
197+ }
140198
141199 // Progress Progressbar
142200 progressBar1 . PerformStep ( ) ;
143-
144- return finalImagecroped ;
145201 }
146202 catch ( Exception ex )
147203 {
148- // Not simplifing for compatibility reasons.
149- if ( finalImage != null )
150- finalImage . Dispose ( ) ;
151-
152204 throw ex ;
153205 }
154206 finally
155207 {
156- //clean up memory
157- foreach ( System . Drawing . Bitmap image in images )
158- {
159- image . Dispose ( ) ;
160- }
161208 }
162209 }
163210
@@ -183,49 +230,14 @@ private void Button1_Click(object sender, EventArgs e)
183230 }
184231 else
185232 {
186-
187233 // Reset ProgressBar
188234 progressBar1 . Value = 0 ;
189235
190236 // Convert Bitmap
191- Bitmap final = CombineBitmap ( PhotosLoc ) ;
192-
193- // Save Bitmap
194- if ( radioButton1 . Checked )
195- {
196- final . Save ( SaveLoc + ".png" , ImageFormat . Png ) ;
197- }
198- else if ( radioButton2 . Checked )
199- {
200- // Ensure we grab the highest possible encoder settings for jps.
201- var encoder = ImageCodecInfo . GetImageEncoders ( ) . First ( c => c . FormatID == ImageFormat . Jpeg . Guid ) ;
202- var encParams = new EncoderParameters ( ) { Param = new [ ] { new EncoderParameter ( Encoder . Quality , 100L ) } } ;
203- final . Save ( SaveLoc + ".jpg" , encoder , encParams ) ;
204- }
205- else if ( radioButton3 . Checked )
206- {
207- final . Save ( SaveLoc + ".bmp" , ImageFormat . Bmp ) ;
208- }
209- else if ( radioButton4 . Checked )
210- {
211- final . Save ( SaveLoc + ".emf" , ImageFormat . Emf ) ;
212- }
213- else if ( radioButton5 . Checked )
214- {
215- final . Save ( SaveLoc + ".ico" , ImageFormat . Icon ) ;
216- }
217- else if ( radioButton6 . Checked )
218- {
219- final . Save ( SaveLoc + ".gif" , ImageFormat . Gif ) ;
220- }
221- else if ( radioButton7 . Checked )
222- {
223- final . Save ( SaveLoc + ".wmf" , ImageFormat . Wmf ) ;
224- }
237+ CombineBitmap ( PhotosLoc ) ;
225238
226239 // Job Finished
227240 MessageBox . Show ( "Stitch Has Completed!" ) ;
228-
229241 }
230242 }
231243
@@ -275,8 +287,10 @@ private void Button3_Click(object sender, EventArgs e)
275287 }
276288
277289 }
278-
279290 }
291+ #endregion
292+
293+ #region Helper Classes
280294
281295 static class Helper
282296 {
@@ -294,7 +308,6 @@ public static string GetUntilOrEmpty(this string text, string stopAt = "-")
294308
295309 return String . Empty ;
296310 }
297-
298- #endregion
299311 }
312+ #endregion
300313}
0 commit comments