44using System . Data ;
55using System . Diagnostics ;
66using System . Drawing ;
7+ using System . Drawing . Drawing2D ;
78using System . Drawing . Imaging ;
89using System . IO ;
910using System . Linq ;
@@ -111,6 +112,7 @@ void LoadAll()
111112
112113
113114 numericUpDownMargin . Value = Convert . ToDecimal ( Properties . Settings . Default . margin ) ;
115+ numericUpDownRotation . Value = Convert . ToDecimal ( Properties . Settings . Default . watermarkRot ) ;
114116 trackBarOpacity . Value = Properties . Settings . Default . opacity ;
115117 trackBarScale . Value = Properties . Settings . Default . watermarkScale ;
116118
@@ -165,6 +167,47 @@ enum WatermarkPosition
165167 WatermarkMode mode = WatermarkMode . WholeFolder ;
166168 WatermarkPosition position = WatermarkPosition . BottomLeft ;
167169
170+
171+ public static Image RotateImage ( Image image , float angle )
172+ {
173+ // Calculate the size of the rotated image
174+ double radians = angle * Math . PI / 180 ;
175+ double cos = Math . Abs ( Math . Cos ( radians ) ) ;
176+ double sin = Math . Abs ( Math . Sin ( radians ) ) ;
177+ int width = ( int ) ( image . Width * cos + image . Height * sin ) ;
178+ int height = ( int ) ( image . Width * sin + image . Height * cos ) ;
179+
180+ // Create a new empty bitmap to hold rotated image
181+ Bitmap rotatedImage = new Bitmap ( width , height ) ;
182+
183+ // Make a graphics object from the empty bitmap
184+ using ( Graphics g = Graphics . FromImage ( rotatedImage ) )
185+ {
186+ // Clear the image with transparent background
187+ g . Clear ( Color . Transparent ) ;
188+
189+ // Set the interpolation mode to high quality
190+ g . InterpolationMode = System . Drawing . Drawing2D . InterpolationMode . HighQualityBicubic ;
191+
192+ // Translate the image to the center of the output bitmap
193+ g . TranslateTransform ( rotatedImage . Width / 2 , rotatedImage . Height / 2 ) ;
194+
195+ // Rotate the image
196+ g . RotateTransform ( angle ) ;
197+
198+ // Translate the image back to its original position
199+ g . TranslateTransform ( - image . Width / 2 , - image . Height / 2 ) ;
200+
201+ // Draw the image onto the graphics object
202+ g . DrawImage ( image , 0 , 0 , image . Width , image . Height ) ;
203+ }
204+
205+ // Return the image
206+ return ( Image ) rotatedImage ;
207+ }
208+
209+
210+
168211 private void AddWatermark ( String imagePath , String watermarkImagePath , float opacity , float scale , int margin , String suffix , String folderName )
169212 {
170213 String fullPathImg = Path . GetFullPath ( imagePath ) ;
@@ -185,7 +228,7 @@ private void AddWatermark(String imagePath, String watermarkImagePath, float opa
185228
186229 // Load the source image and the watermark image
187230 using ( Image image = Image . FromFile ( imagePath ) )
188- using ( Image watermarkImage = Image . FromFile ( watermarkImagePath ) )
231+ using ( Image watermarkImage = RotateImage ( Image . FromFile ( watermarkImagePath ) , Convert . ToSingle ( numericUpDownRotation . Value ) ) )
189232 {
190233 // Calculate the scale factor for the watermark
191234 float finalScale = scale ;
@@ -236,9 +279,6 @@ private void AddWatermark(String imagePath, String watermarkImagePath, float opa
236279 //int ccc = 0;
237280 while ( y > ( height + margin ) * - 1 )
238281 {
239- //Console.WriteLine($"{x}, {y}");
240- //ccc++;
241- //Console.WriteLine(ccc);
242282 // Draw the watermark image onto the source image using the DrawImage method
243283 imageGraphics . DrawImage ( resizedWatermark , new Rectangle ( x , y , width , height ) ,
244284 0 , 0 , width , height , GraphicsUnit . Pixel , imageAttributes ) ;
@@ -253,9 +293,6 @@ private void AddWatermark(String imagePath, String watermarkImagePath, float opa
253293 //ccc = 0;
254294 while ( y < ( height + margin ) + image . Height )
255295 {
256- //Console.WriteLine($"{x}, {y}");
257- //ccc++;
258- //Console.WriteLine(ccc);
259296 // Draw the watermark image onto the source image using the DrawImage method
260297 imageGraphics . DrawImage ( resizedWatermark , new Rectangle ( x , y , width , height ) ,
261298 0 , 0 , width , height , GraphicsUnit . Pixel , imageAttributes ) ;
@@ -277,9 +314,6 @@ private void AddWatermark(String imagePath, String watermarkImagePath, float opa
277314 //int ccc = 0;
278315 while ( y > ( height + margin ) * - 1 )
279316 {
280- //Console.WriteLine($"{x}, {y}");
281- //ccc++;
282- //Console.WriteLine(ccc);
283317 // Draw the watermark image onto the source image using the DrawImage method
284318 imageGraphics . DrawImage ( resizedWatermark , new Rectangle ( x , y , width , height ) ,
285319 0 , 0 , width , height , GraphicsUnit . Pixel , imageAttributes ) ;
@@ -294,9 +328,6 @@ private void AddWatermark(String imagePath, String watermarkImagePath, float opa
294328 //ccc = 0;
295329 while ( y < ( height + margin ) + image . Height )
296330 {
297- //Console.WriteLine($"{x}, {y}");
298- //ccc++;
299- //Console.WriteLine(ccc);
300331 // Draw the watermark image onto the source image using the DrawImage method
301332 imageGraphics . DrawImage ( resizedWatermark , new Rectangle ( x , y , width , height ) ,
302333 0 , 0 , width , height , GraphicsUnit . Pixel , imageAttributes ) ;
@@ -335,7 +366,7 @@ private void AddWatermark(String imagePath, String watermarkImagePath, Watermark
335366
336367 // Load the source image and the watermark image
337368 using ( Image image = Image . FromFile ( imagePath ) )
338- using ( Image watermarkImage = Image . FromFile ( watermarkImagePath ) )
369+ using ( Image watermarkImage = RotateImage ( Image . FromFile ( watermarkImagePath ) , Convert . ToSingle ( numericUpDownRotation . Value ) ) )
339370 {
340371 // Calculate the scale factor for the watermark
341372 float finalScale = scale ;
@@ -770,6 +801,7 @@ private void btnStart_Click(object sender, EventArgs e)
770801 break ;
771802 }
772803 Properties . Settings . Default . margin = Convert . ToInt32 ( numericUpDownMargin . Value ) ;
804+ Properties . Settings . Default . watermarkRot = Convert . ToInt32 ( numericUpDownRotation . Value ) ;
773805 Properties . Settings . Default . opacity = trackBarOpacity . Value ;
774806 Properties . Settings . Default . watermarkScale = trackBarScale . Value ;
775807
@@ -840,6 +872,12 @@ private void numericUpDownMargin_ValueChanged(object sender, EventArgs e)
840872 Properties . Settings . Default . Save ( ) ;
841873 }
842874
875+ private void numericUpDownRotation_ValueChanged ( object sender , EventArgs e )
876+ {
877+ Properties . Settings . Default . watermarkRot = Convert . ToInt32 ( numericUpDownRotation . Value ) ;
878+ Properties . Settings . Default . Save ( ) ;
879+ }
880+
843881 private void tbFileFolder_DragDrop ( object sender , DragEventArgs e )
844882 {
845883
0 commit comments