-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingWindow.cs
More file actions
61 lines (57 loc) · 2.06 KB
/
Copy pathSettingWindow.cs
File metadata and controls
61 lines (57 loc) · 2.06 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
namespace PrizeDrawTool
{
public partial class SettingWindow : Form
{
MainWindow mainWindow;
public SettingWindow(MainWindow mw)
{
InitializeComponent();
mainWindow = mw;
}
private void ClearBannerBtn_Click(object sender, EventArgs e)
{
DeleteBannerImage();
}
private void BannerSettingBtn_Click( object sender, EventArgs e )
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "jpeg files (*.jpg, *.jpeg) | *.jpg; *.jpeg | PNG files (*.png) | *.png";
if (ofd.ShowDialog() == DialogResult.OK) //弹出窗口选择文件
{
string fileName = ofd.FileName;
ofd.Dispose();
DeleteBannerImage();
string filePath = $".\\bannerimg{Path.GetExtension(fileName)}";
File.Copy(fileName, filePath, overwrite: true);
var img = Image.FromFile(filePath);
mainWindow.BannerPictureBox.Image = img;
}
}
private void DeleteBannerImage()
{
if (mainWindow.BannerPictureBox.Image != null)
{
mainWindow.BannerPictureBox.Image.Dispose();
mainWindow.BannerPictureBox.Image = null;
}
// 删除文件
string[] fileNames = { ".\\bannerimg.jpg", ".\\bannerimg.jpeg", ".\\bannerimg.png" };
foreach (var fileName in fileNames)
{
if (File.Exists(fileName))
{
try
{
File.Delete(fileName);
mainWindow.BannerPictureBox.Image = Properties.Resources.default_banner;
}
catch (IOException ex)
{
MessageBox.Show($"无法删除文件 {fileName},原因:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
}