-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageGeneration.cs
96 lines (86 loc) · 3.72 KB
/
ImageGeneration.cs
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
90
91
92
93
94
95
96
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace DarkNotepad
{
internal class ImageGeneration
{
bool failedToGenerate = false;
// 'GenerateImage()' uses a texture composited from black and white pixels.
// The darker the pixels are the more 'c2' would override 'c1'.
// This is used for custom icons when the user changes themes.
// It then saves the BitMap image to a .png file in a 'temp' folder.
public string GenerateImage(Bitmap mask,Color c1, Color c2, string name)
{
try
{
Rectangle rect = new Rectangle(0, 0, mask.Width, mask.Height);
Bitmap bm = new Bitmap(rect.Width, rect.Height);
Graphics g = Graphics.FromImage(bm);
Brush b = new SolidBrush(c1);
g.FillRectangle(b, rect);
for (int i = 0; i < bm.Width; i++)
for (int j = 0; j < bm.Height; j++)
{
Color mask_c = mask.GetPixel(i, j);
int alpha_Value = (mask_c.R + mask_c.G + mask_c.B) / 3;
float mask_Value = 0f;
if (alpha_Value == 0)
{
mask_Value = 0f;
}
else
{
mask_Value = (float)alpha_Value / (float)255;
}
Color new_c = Color.FromArgb(
(int)(c1.R * mask_Value + c2.R * (1 - mask_Value)),
(int)(c1.G * mask_Value + c2.G * (1 - mask_Value)),
(int)(c1.B * mask_Value + c2.B * (1 - mask_Value)));
bm.SetPixel(i, j, new_c);
}
string temp_folder_path = Path.Combine(Environment.GetEnvironmentVariable("TEMP"),"DarkNotepad-000Daniel-temp");
if (!Directory.Exists(temp_folder_path))
{
Directory.CreateDirectory(temp_folder_path);
}
string image_path = Path.Combine(temp_folder_path, name + ".png");
bm.Save(@image_path, System.Drawing.Imaging.ImageFormat.Png);
bm.Dispose();
return image_path;
}
catch
{
failedToGenerate = true;
return null;
}
}
// This 'LoadImage()' loads the generated images by name.
public Bitmap LoadImage(string name)
{
try
{
string temp_folder_path = Path.Combine(Environment.GetEnvironmentVariable("TEMP"),"DarkNotepad-000Daniel-temp");
if (!Directory.Exists(temp_folder_path) || !File.Exists(Path.Combine(temp_folder_path, name + ".png")))
{
if (failedToGenerate)
{
return Resource1.Missing_Image;
}
Application.OpenForms.OfType<Notepad>().First().CreateCustomIcons();
}
string image_path = Path.Combine(temp_folder_path, name + ".png");
var bitmap_ms = new MemoryStream(File.ReadAllBytes(image_path));
Bitmap loaded_bitmap = (Bitmap)Bitmap.FromStream(bitmap_ms);
bitmap_ms.Dispose();
return loaded_bitmap;
}
catch
{
return Resource1.Missing_Image;
}
}
}
}