-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
263 lines (214 loc) · 7.26 KB
/
Form1.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net.Http;
namespace MyWallpaperManager
{
public partial class Form1 : Form
{
private readonly List<WallpaperModel> wallpapers = new List<WallpaperModel>();
public class WallpaperModel
{
public string ImagePath { get; set; }
public bool IsDownloaded { get; set; }
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
const int SPI_SETDESKWALLPAPER = 20; // Value set wallpaper
const int SPIF_UPDATEINIFILE = 0x01; // Save parameters in config user file;
const int SPIF_SENDCHANGE = 0x02; // Notify apps change
private void SetWallpaper(string wallpaper)
{
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, wallpaper, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
}
public Form1()
{
InitializeComponent();
this.FormClosed += ClosedForm;
// Visualize correctly wallpaper
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
string[] wallpapersLines = File.ReadAllLines("wallpapers.txt");
List<WallpaperModel> listWallpapers = GetWallpapers(wallpapersLines);
// Save wallpapers in app
foreach (WallpaperModel wallpaper in listWallpapers)
{
wallpapers.Add(wallpaper);
listBox1.Items.Add(Path.GetFileName(wallpaper.ImagePath));
}
// Create a folder for storage wallpapers
if (!Directory.Exists(@".\images")) Directory.CreateDirectory(@".\images");
}
// Add button
private void AddButtonClick(object sender, EventArgs e)
{
// Select file window
OpenFileDialog openFileDialog = new OpenFileDialog()
{
Filter = " Image files: (*.jpg; *.jpeg; *.png;) | *.jpg; *.jpeg; *.png;"
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string selectedFile = openFileDialog.FileName;
WallpaperModel newWallpaper = new WallpaperModel()
{
ImagePath = selectedFile,
IsDownloaded = false
};
wallpapers.Add(newWallpaper);
listBox1.Items.Add(Path.GetFileName(newWallpaper.ImagePath));
}
}
// Remove button
private void RemoveButtonClick(object sender, EventArgs e)
{
if (listBox1.SelectedIndex < 0) return;
if (wallpapers.ElementAt(listBox1.SelectedIndex).IsDownloaded)
{
try
{
pictureBox1.Image.Dispose(); // ! important, first dispose
pictureBox1.Image = null; // after null
// Delete wallpaper in directory
File.Delete(wallpapers.ElementAt(listBox1.SelectedIndex).ImagePath);
}
catch (Exception)
{
MessageBox.Show("There was an error trying to download the file.");
}
}
// Remove wallpaper view
pictureBox1.Image = null;
wallpapers.RemoveAt(listBox1.SelectedIndex);
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
// Select wallpaper
private void ListBoxSelect(object sender, EventArgs e)
{
if (listBox1.SelectedIndex < 0) return;
pictureBox1.Image = new Bitmap(wallpapers.ElementAt(listBox1.SelectedIndex).ImagePath);
}
// Set wallpaper
private void SetWallpaperClick(object sender, EventArgs e)
{
if (listBox1.SelectedIndex < 0) return;
SetWallpaper(wallpapers[listBox1.SelectedIndex].ImagePath);
}
// Download wallpaper
private async void DownloadButtonClick(object sender, EventArgs e)
{
// Get files count
int imagesInPicturesFolder = Directory.GetFiles(@".\images")
.Where(x =>
x.Contains(".jpeg") ||
x.Contains(".jpg") ||
x.Contains(".png") ||
x.Contains(".webp"))
.Count();
string pathToDownloadImage = Path.Combine(Environment.CurrentDirectory, "images", $"downloaded_image_{imagesInPicturesFolder + 1}.jpg");
if (textBox1.Text.Contains(".webp"))
{
MessageBox.Show("Webp formats are not supported");
}
if (textBox1.Text.Length > 0)
{
bool downloadImage = await DownloadWallpaperAsync(textBox1.Text, pathToDownloadImage);
// Check downloaded image
if (!downloadImage)
{
MessageBox.Show("The image could not be downloaded, please check the URL.");
return;
}
}
else
{
MessageBox.Show("Enter a valid URL.");
return;
}
WallpaperModel newWallpaper = new WallpaperModel()
{
ImagePath = pathToDownloadImage,
IsDownloaded = true
};
wallpapers.Add(newWallpaper);
listBox1.Items.Add(Path.GetFileName(pathToDownloadImage));
DialogResult alert = MessageBox.Show("Image downloaded successfully.");
}
// Get wallpaper
private WallpaperModel GetWallpaper(string wallpaper)
{
string[] dataWallpaper = wallpaper.Split('|');
if (dataWallpaper.Length == 2)
{
WallpaperModel newWallpaper = new WallpaperModel
{
ImagePath = dataWallpaper[0],
IsDownloaded = dataWallpaper[1] == "true"
};
return newWallpaper;
}
return null;
}
// Get wallpapers
private List<WallpaperModel> GetWallpapers(string[] wallpapers)
{
List<WallpaperModel> listWallpapers = new List<WallpaperModel>();
foreach (string wallpaper in wallpapers)
{
WallpaperModel newWallpaper = GetWallpaper(wallpaper);
if (newWallpaper is null) break;
listWallpapers.Add(newWallpaper);
}
return listWallpapers;
}
// Download wallpaper
private async Task<bool> DownloadWallpaperAsync(string url, string savePath)
{
using (HttpClient client = new HttpClient())
{
try
{
// Using http client
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
byte[] imageBytes = await response.Content.ReadAsByteArrayAsync();
File.WriteAllBytes(savePath, imageBytes);
return true;
}
catch (InvalidOperationException)
{
return false;
}
catch (ArgumentException)
{
return false;
}
catch (Exception)
{
throw;
}
}
}
// Form closed
private void ClosedForm(object sender, FormClosedEventArgs e)
{
using (StreamWriter writer = new StreamWriter("wallpapers.txt"))
{
foreach (WallpaperModel wallpaper in wallpapers)
{
StringBuilder line = new StringBuilder();
line.Append(wallpaper.ImagePath);
line.Append("|");
line.Append(wallpaper.IsDownloaded.ToString());
writer.WriteLine(line);
}
}
}
}
}