Skip to content

Commit 42ef887

Browse files
committed
Watermark Rotation Support
Added support for applying a rotation to the Watermark
1 parent 4e23796 commit 42ef887

File tree

6 files changed

+156
-26
lines changed

6 files changed

+156
-26
lines changed

Watermarker/App.config

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,52 @@
22
<configuration>
33
<configSections>
44
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
5+
<section name="Simple_Image_Watermarker.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
56
<section name="Watermarker.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
67
</sectionGroup>
78
</configSections>
89
<startup>
910
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
1011
</startup>
1112
<userSettings>
13+
<Simple_Image_Watermarker.Properties.Settings>
14+
<setting name="UpgradeRequired" serializeAs="String">
15+
<value>True</value>
16+
</setting>
17+
<setting name="mode" serializeAs="String">
18+
<value>1</value>
19+
</setting>
20+
<setting name="position" serializeAs="String">
21+
<value>9</value>
22+
</setting>
23+
<setting name="suffix" serializeAs="String">
24+
<value>_wm</value>
25+
</setting>
26+
<setting name="watermarkedFolderName" serializeAs="String">
27+
<value>watermarked</value>
28+
</setting>
29+
<setting name="watermarkImagePath" serializeAs="String">
30+
<value />
31+
</setting>
32+
<setting name="ImagePath" serializeAs="String">
33+
<value />
34+
</setting>
35+
<setting name="FolderPath" serializeAs="String">
36+
<value />
37+
</setting>
38+
<setting name="margin" serializeAs="String">
39+
<value>10</value>
40+
</setting>
41+
<setting name="opacity" serializeAs="String">
42+
<value>10</value>
43+
</setting>
44+
<setting name="watermarkScale" serializeAs="String">
45+
<value>100</value>
46+
</setting>
47+
<setting name="watermarkRot" serializeAs="String">
48+
<value>0</value>
49+
</setting>
50+
</Simple_Image_Watermarker.Properties.Settings>
1251
<Watermarker.Properties.Settings>
1352
<setting name="UpgradeRequired" serializeAs="String">
1453
<value>True</value>

Watermarker/Frm_Main.Designer.cs

Lines changed: 42 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Watermarker/Frm_Main.cs

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Data;
55
using System.Diagnostics;
66
using System.Drawing;
7+
using System.Drawing.Drawing2D;
78
using System.Drawing.Imaging;
89
using System.IO;
910
using 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

Watermarker/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0")]
35+
[assembly: AssemblyVersion("1.1.0")]
36+
[assembly: AssemblyFileVersion("1.1.0")]

Watermarker/Properties/Settings.Designer.cs

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Watermarker/Properties/Settings.settings

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version='1.0' encoding='utf-8'?>
2-
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="Watermarker.Properties" GeneratedClassName="Settings">
2+
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="Simple_Image_Watermarker.Properties" GeneratedClassName="Settings">
33
<Profiles />
44
<Settings>
55
<Setting Name="UpgradeRequired" Type="System.Boolean" Scope="User">
@@ -35,5 +35,8 @@
3535
<Setting Name="watermarkScale" Type="System.Int32" Scope="User">
3636
<Value Profile="(Default)">100</Value>
3737
</Setting>
38+
<Setting Name="watermarkRot" Type="System.Int32" Scope="User">
39+
<Value Profile="(Default)">0</Value>
40+
</Setting>
3841
</Settings>
3942
</SettingsFile>

0 commit comments

Comments
 (0)