-
Notifications
You must be signed in to change notification settings - Fork 4
/
InkControls.cs
217 lines (171 loc) · 7.76 KB
/
InkControls.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.ComponentModel;
namespace NDI_Telestrator
{
public static class InkControls
{
private static WhiteboardCanvas _whiteboard;
public static WhiteboardCanvas whiteboard
{
get
{
return _whiteboard;
}
set
{
_whiteboard = value;
OnPropertyChanged();
}
}
private static void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(null, new PropertyChangedEventArgs(name));
}
public static event PropertyChangedEventHandler PropertyChanged;
public static void onBtnSaveClick(object sender, RoutedEventArgs e)
{
System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog();
saveDialog.Filter = "Telestrator File (*.tls)|*.tls";
if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FileStream fs = new FileStream(saveDialog.FileName, FileMode.Create);
foreach (InkLayer layer in whiteboard.Children) layer.Strokes.Save(fs);
fs.Close();
}
}
private static List<System.Windows.Ink.StrokeCollection> requestOpenFile()
{
List<System.Windows.Ink.StrokeCollection> layers = new List<System.Windows.Ink.StrokeCollection>();
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Filter = "Ink Stroke File (*.isf)|*.isf|Telestrator File (*.tls)|*.tls|All supported files|*.isf;*.tls";
openDialog.FilterIndex = 3;
if (openDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return null;
FileStream fs = new FileStream(openDialog.FileName, FileMode.Open);
while (fs.Position != fs.Length) layers.Add(new System.Windows.Ink.StrokeCollection(fs));
fs.Close();
return layers;
}
public static void requestAddDrawing(bool asMultipleLayers)
{
List<System.Windows.Ink.StrokeCollection> layers = requestOpenFile();
if (layers == null || layers.Count == 0) return;
if (asMultipleLayers)
{
foreach (System.Windows.Ink.StrokeCollection layer in layers) whiteboard.addNewLayer(layer);
}
else
{
System.Windows.Ink.StrokeCollection sum = new System.Windows.Ink.StrokeCollection();
foreach (System.Windows.Ink.StrokeCollection layer in layers) sum.Add(layer);
whiteboard.addNewLayer(sum);
}
}
public static void onBtnLoadClick(object sender, RoutedEventArgs e)
{
List<System.Windows.Ink.StrokeCollection> layers = requestOpenFile();
if (layers == null || layers.Count == 0) return;
whiteboard.ResetState(false);
foreach (System.Windows.Ink.StrokeCollection layer in layers) whiteboard.addNewLayer(layer);
}
public static void setPenColour(Color colour)
{
whiteboard.SetPenColour(colour);
}
public static void setBackgroundColour(Brush colour)
{
whiteboard.Background = colour;
}
public static void clearWhiteboard()
{
whiteboard.Clear();
}
public static void setPenThickness(double size)
{
whiteboard.SetPenThickness(size);
}
public static void undo()
{
whiteboard.Undo();
// Previews
//ABC.Children.Clear();
//for (int i = 1; i < theWhiteboard.inkCanvas.Strokes.Count; i++)
//{
// List<System.Windows.Ink.Stroke> a = new List<System.Windows.Ink.Stroke>(theWhiteboard.inkCanvas.Strokes);
// int offset = 0;
// int amt = i;
// a = a.GetRange(offset, System.Math.Max(0, System.Math.Min(amt, a.Count - offset)));
// Image img = new Image();
// img.Width = 640;
// img.Height = 360;
// img.Source = Draw(new System.Windows.Ink.StrokeCollection(a));
// ABC.Children.Add(img);
//}
}
public static void redo()
{
whiteboard.Redo();
}
// Draw a selective number of layers
public static BitmapFrame Draw(System.Windows.Ink.StrokeCollection[] layers, Brush background = null)
{
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
if (background != null) drawingContext.DrawRectangle(background, null, new Rect(0, 0, (int)whiteboard.activeInkCanvas.Width, (int)whiteboard.activeInkCanvas.Height));
foreach (System.Windows.Ink.StrokeCollection strokes in layers) strokes.Draw(drawingContext);
drawingContext.Close();
RenderTargetBitmap rtb = new RenderTargetBitmap((int)whiteboard.activeInkCanvas.Width, (int)whiteboard.activeInkCanvas.Height, 96d, 96d, System.Windows.Media.PixelFormats.Default);
rtb.Render(drawingVisual);
return BitmapFrame.Create(rtb);
}
}
public static void Btn_Screenshot_Click(object sender, RoutedEventArgs e)
{
Enums.ScreenshotFormatTypes type = Options.screenshotFormatType;
string saveFileName = "Screenshot " + DateTime.Now.ToString("yyyyMMdd-HHmmss");
if (!Options.quickSaveEnabled)
{
System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog();
saveDialog.Filter = "JPG|*.jpg|PNG|*.png";
saveDialog.FilterIndex = (int)Options.screenshotFormatType + 1; // Indexing is 1-based
System.Windows.Forms.DialogResult result = saveDialog.ShowDialog();
if (result != System.Windows.Forms.DialogResult.OK) return;
saveFileName = saveDialog.FileName;
type = (Enums.ScreenshotFormatTypes)(saveDialog.FilterIndex - 1);
string lowerCase = saveDialog.FileName.ToLower();
if (lowerCase.EndsWith(".jpg")) type = Enums.ScreenshotFormatTypes.JPG;
else if (lowerCase.EndsWith(".png")) type = Enums.ScreenshotFormatTypes.PNG;
else saveFileName += (saveDialog.FilterIndex == 1 ? ".jpg" : ".png");
}
else saveFileName += type == Enums.ScreenshotFormatTypes.JPG ? ".jpg" : ".png";
BitmapFrame b;
if (type == Enums.ScreenshotFormatTypes.JPG)
{
b = whiteboard.Draw(whiteboard.Background == Brushes.Transparent ? Brushes.White : whiteboard.Background);
JpegBitmapEncoder j = new JpegBitmapEncoder();
j.Frames.Add(b);
using (var file = new FileStream(saveFileName, FileMode.Create)) j.Save(file);
}
else
{
b = whiteboard.Draw(Brushes.Transparent);
PngBitmapEncoder p = new PngBitmapEncoder();
p.Frames.Add(b);
using (var file = new FileStream(saveFileName, FileMode.Create)) p.Save(file);
}
}
public static void createNewLayer()
{
whiteboard.addNewLayer();
}
public static void setActiveLayer(int index)
{
whiteboard.setActive(index);
}
}
}