This repository was archived by the owner on Feb 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathFormShowFrames.cs
More file actions
143 lines (118 loc) · 4.64 KB
/
FormShowFrames.cs
File metadata and controls
143 lines (118 loc) · 4.64 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
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace WebMCam
{
public partial class FormShowFrames : Form
{
public bool Result = false;
public int framesCount = 0;
private string framesPath;
private string imageExtension;
private bool allowDeletion;
private List<string> markedFrames = new List<string>();
public FormShowFrames(string framesPath, ImageFormat imageFormat, bool allowDeletion = true)
{
this.framesPath = framesPath;
this.imageExtension = "." + imageFormat.ToString().ToLower();
this.allowDeletion = allowDeletion;
InitializeComponent();
}
private void formShowFrames_Load(object sender, EventArgs e)
{
// Get all frames from framesPath directory
List<FileInfo> framesList = new DirectoryInfo(framesPath)
.GetFiles("*" + imageExtension).OrderBy(f => f.LastWriteTime).ToList();
// Return if no frames are found
if (framesList.Count < 1)
{
Result = false;
Close();
return;
}
// Add frames to listbox
foreach (FileInfo frame in framesList)
listBoxFrames.Items.Add(frame.Name.Substring(1).Split('.')[0]);
listBoxFrames.SetSelected(0, true);
}
private void listBoxFrames_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBoxFrames.SelectedIndex < -1)
return;
var path = Path.Combine(framesPath, "_" + listBoxFrames.SelectedItem + imageExtension);
// Be sure file exists before deleting it
if (!File.Exists(path))
return;
// Set the preview box to the image
using (var bmp = new Bitmap(path))
pictureBoxFrame.Image = new Bitmap(bmp);
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!allowDeletion)
{
MessageBox.Show("Cannot delete frames when audio is recorded.", "Notice",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
// Loop MUST be inverse otherwise it will delete the wrong frames
for (var i = listBoxFrames.SelectedItems.Count - 1; i > -1; i--)
{
var frame = listBoxFrames.SelectedItems[i];
var path = Path.Combine(framesPath, "_" + frame + imageExtension);
listBoxFrames.Items.Remove(frame);
// Be sure file exists before deleting it
if (File.Exists(path))
File.Delete(path);
}
}
private void markToolStripMenuItem_Click(object sender, EventArgs e)
{
for (var i = listBoxFrames.SelectedItems.Count - 1; i > -1; i--)
{
var frame = listBoxFrames.SelectedItems[i].ToString();
if (markedFrames.Contains(frame))
{
markedFrames.Remove(listBoxFrames.SelectedItems[i].ToString());
}
else
{
markedFrames.Add(listBoxFrames.SelectedItems[i].ToString());
}
}
listBoxFrames.Refresh();
}
private void buttonDone_Click(object sender, EventArgs e)
{
List<FileInfo> framesList = new DirectoryInfo(framesPath)
.GetFiles("*" + imageExtension).OrderBy(f => f.LastWriteTime).ToList();
int i = 0;
foreach(var frame in framesList)
{
frame.MoveTo(Path.Combine(framesPath, i.ToString() + imageExtension));
i++;
}
framesCount = framesList.Count;
Result = true;
Close();
}
private void listBoxFrames_KeyDown(object sender, KeyEventArgs e)
{
// Delete Key
if (e.KeyValue == (char)Keys.Delete)
deleteToolStripMenuItem_Click(sender, e);
}
private void listBoxFrames_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
var text = ((ListBox)sender).Items[e.Index].ToString();
Brush brush = markedFrames.Contains(text) ? Brushes.Red : Brushes.Black;
e.Graphics.DrawString(text, e.Font, brush, e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
}
}