-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWatermark.cs
More file actions
89 lines (83 loc) · 2.25 KB
/
Copy pathWatermark.cs
File metadata and controls
89 lines (83 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Watermark3
{
interface IWorker
{
void Notify(string message);
}
class Watermark : IWorker
{
public List<ILogger> Loggers { get; private set; }
public string WatermarkPath { get; private set; }
public Watermark(string watermarkPath)
{
Loggers = new List<ILogger>();
WatermarkPath = watermarkPath;
}
public void AddWatermark(string imagePath)
{
using (Image image = Image.FromFile(imagePath))
using (Image watermark = Image.FromFile(WatermarkPath))
using (TextureBrush brush = new TextureBrush(watermark))
{
Graphics graphics = null;
try
{
graphics = Graphics.FromImage(image);
}
catch (Exception ex)
{
//here we handle indexed pixels
Notify($"Image {imagePath} has indexed pixels.");
Console.WriteLine(ex.Message);
Bitmap tempImage = new Bitmap(image.Width, image.Height);
graphics = Graphics.FromImage(tempImage);
graphics.DrawImage(image, 0, 0);
}
finally
{
int x = image.Width - 10 - watermark.Width;
int y = image.Height - 10 - watermark.Height;
brush.TranslateTransform(x, y);
graphics.FillRectangle(brush, new Rectangle(x, y, watermark.Width, watermark.Height));
image.Save(GetNewFileName(imagePath));
}
}
}
public void MarkAllFiles(string[] filePaths)
{
if (filePaths.Length == 0)
{
Notify("No files to watermark.");
return;
}
for (int i = 0; i < filePaths.Length; i++)
{
AddWatermark(filePaths[i]);
}
}
private string GetNewFileName(string fileName)
{
string extension = Path.GetExtension(fileName);
string newFileName = "w_" + Path.GetFileNameWithoutExtension(fileName) + extension;
string directory = Path.GetDirectoryName(fileName);
string newFullName = Path.Combine(directory, newFileName);
File.Create(newFullName).Close();
Notify($"File {newFullName} created.");
return newFullName;
}
public void Notify(string message)
{
for (int i = 0; i < Loggers.Count; i++)
{
Loggers[i].Log(message);
}
}
}
}