-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6.html
More file actions
323 lines (323 loc) · 11.4 KB
/
day6.html
File metadata and controls
323 lines (323 loc) · 11.4 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!DOCTYPE HTML>
<html lang="de">
<head>
<title>Programming C#</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=1274, user-scalable=no">
<link rel="stylesheet" href="themes/styles/style.css">
</head>
<body class="list">
<header class="caption">
<h1>Programming C#</h1>
</header>
<div class="slide cover" id="Cover"><div>
<section>
<header>
<h2>Programming C#</h2>
<h3>Day 6: Drawing with GDI+, exception handling, resource management</h3>
</header>
<img src="pictures/cover.jpg" alt="">
</section>
</div></div>
<div class="slide" id="overview"><div>
<section>
<header>
<h2>Content</h2>
</header>
<ul>
<li>Drawing with GDI+</li>
<li>Images and image formats</li>
<li>User control drawing</li>
<li>Handling exceptions</li>
<li>Dynamic programming with C#</li>
<li>object, var and dynamic</li>
<li>Reflection</li>
</ul>
</section>
</div></div>
<div class="slide" id="drawing-gdi-plus"><div>
<section>
<header>
<h2>Drawing with GDI+</h2>
</header>
<ul>
<li>Windows has a drawing API called GDI</li>
<li>Windows Forms extends this API to GDI+</li>
<li>The central object is called <code>Graphics</code></li>
<li>This object requires a target</li>
<li>This can be either an image or the screen</li>
<li>Images have advantages (buffering, modifying, ...)</li>
<li>Be aware that this requires a lot of memory</li>
</ul>
</section>
</div></div>
<div class="slide" id="gdi-plus-methods-1"><div>
<section>
<header>
<h2>GDI+ methods (pt. 1)</h2>
</header>
<ul>
<li>Creating an image: <code>var bmp = new Bitmap(400, 300)</code></li>
<li>Using it: <code>var g = Graphics.FromImage(bmp)</code></li>
<li>Now we have access to methods that start with Draw and Fill</li>
<li>Draw / fill methods draw / fill the shape (border / content)</li>
<li>Drawing is done by a <code>Pen</code> (width, color)</li>
<li>Filling is done by a <code>Brush</code> (px→color)</li>
<li>Basic brush: <code>SolidColorBrush</code></li>
<li>More advanced: <code>LinearGradientBrush</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="gdi-plus-methods-2"><div>
<section>
<header>
<h2>GDI+ methods (pt. 2)</h2>
</header>
<ul>
<li>e.g. <code>DrawRectangle()</code> draws the border of a rectangle</li>
<li>e.g. <code>FillEllipse()</code> fills an ellipse (could be circle)</li>
<li>There are more advanced concepts like transformations</li>
<li>Here we set transformation matrices (translate, rotate, scale)</li>
<li>Also (quite complex) paths can be done</li>
<li>Such paths can create regions</li>
<li>We can apply operators (like union) on regions</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-drawing-gdi-plus"><div>
<section>
<header>
<h2 class="example">→ Example - Drawing with GDI+</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day7/GdiPlus" title="Download source" class="example-download">GdiPlus.zip</a>
</section>
</div></div>
<div class="slide" id="images"><div>
<section>
<header>
<h2>Images and image formats</h2>
</header>
<ul>
<li>There is a central abstract <code>Image</code> class</li>
<li>A more specialized version is the <code>Bitmap</code> class</li>
<li>The .NET-Framework knows several image formats (bmp, jpg, ...)</li>
<li>Images can easily be read or written to the filesystem</li>
<li><code>Image.FromFile()</code> reads an image to an <code>Image</code></li>
<li>Every instance has a <code>Save()</code> method</li>
</ul>
</section>
</div></div>
<div class="slide" id="user-control-drawing"><div>
<section>
<header>
<h2>Drawing user controls</h2>
</header>
<ul>
<li>Several ways: A good way is to override <code>OnPaint()</code></li>
<li>This method gives us an argument of type <code>PaintEventArgs</code></li>
<li>The property <code>ClipRectangle</code> gives us the boundaries</li>
<li><code>Graphics</code> gives us the graphics pointer</li>
<li>Another way is to create an event handler for the <code>Paint</code> event</li>
<li>Some controls (like the <code>ListBox</code> control) give us a third way</li>
<li>Set the <code>DrawMode</code> and create an event handler (for <code>DrawItem</code>)</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-user-control-drawing"><div>
<section>
<header>
<h2 class="example">→ Example - User control drawing</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day7/GdiControl" title="Download source" class="example-download">GdiControl.zip</a>
</section>
</div></div>
<div class="slide" id="exceptions"><div>
<section>
<header>
<h2>Exceptions</h2>
</header>
<ul>
<li>We should always try to avoid exceptions</li>
<li>However, sometimes exceptions cannot be avoided</li>
<li>Example: Communication with the filesystem - OS throws exception</li>
<li>Hence we need a way to react to these exceptions</li>
<li>Rule: If an exception is possible, we should handle it</li>
<li>The construct is the same as in C++ (or Java)</li>
<li>Every exception inherits from the <code>Exception</code> class</li>
</ul>
</section>
</div></div>
<div class="slide" id="throw-exceptions"><div>
<section>
<header>
<h2>Throwing exceptions</h2>
</header>
<ul>
<li><code>throw</code> will throw an exception or re-throw the current exception</li>
<li>Throwing an exception will end the current code execution</li>
<li>Additionally the exception will bubble up</li>
</ul>
<pre>
<code>void Main() { "Before".Dump(); Sub(); "After".Dump(); }</code>
<code>void Sub() { "Enter".Dump(); throw new Exception("Bug"); }</code>
<code>//Dump() is an extension that is like Console.WriteLine()</code>
</pre>
</section>
</div></div>
<div class="slide" id="catch-exceptions"><div>
<section>
<header>
<h2>Catching exceptions</h2>
</header>
<ul>
<li>So how to handle? Consider a try-catch block</li>
<li><code>try { /* try here */ } catch { /* catch here /* }</code></li>
<li>The try-block contains all (prob. failing) statements</li>
<li>The catch-block will be called if an exception occurred</li>
<li>Such a catch-block catches ALL possible exceptions</li>
<li>If we want or need access write <code>catch (Exception ex) {}</code></li>
<li>Catch specific e.g. <code>catch (FileNotFoundException) {}</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="finally"><div>
<section>
<header>
<h2>Final actions</h2>
</header>
<ul>
<li>Sometimes we have code that depends on opening resources</li>
<li>No matter if an exception occurred or not we want to close those</li>
<li>This is where a <code>finally {}</code> block is useful</li>
<li>Now we can have try-catch, try-catch-finally or just try-finally</li>
<li>Also note that <code>return</code> in one block will still call the finally</li>
</ul>
<pre>
<code>try { "Try has been entered".Dump(); return true; }</code>
<code>finally { "Finally has been called".Dump(); }</code>
</pre>
</section>
</div></div>
<div class="slide" id="example-exceptions"><div>
<section>
<header>
<h2 class="example">→ Example - Exceptions</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day7/Exceptions.cs" title="Download source" class="example-download">Exceptions.cs</a>
</section>
</div></div>
<div class="slide" id="dynamic"><div>
<section>
<header>
<h2>Dynamic programming</h2>
</header>
<ul>
<li>C# and the CLR are static typed</li>
<li>However, the DLR (Dynamic Language Runtime) is not</li>
<li>C# introduces the <code>DynamicObject</code> type</li>
<li>This is our portal to the DLR</li>
<li>We have to tell the compiler explicitely to use dynamic</li>
<li>How to use? <code>dynamic a = 4</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="dynamic-opportunities"><div>
<section>
<header>
<h2>Opportunities</h2>
</header>
<ul>
<li>Easier interaction with dynamic languages</li>
<li>Here mostly JavaScript and Python are mentioned</li>
<li>However, we lose intellisense support</li>
<li>The compiler cannot detect errors</li>
<li>Be aware of possible exceptions during runtime</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-dynamic"><div>
<section>
<header>
<h2 class="example">→ Example - Dynamic</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day5/Dynamic.cs" title="Download source" class="example-download">Dynamic.cs</a>
</section>
</div></div>
<div class="slide" id="object-var-dynamic"><div>
<section>
<header>
<h2>object, var and dynamic</h2>
</header>
<ul>
<li>Never think they are equal!</li>
<li><code>var</code> can be <code>object</code> or <code>dynamic</code></li>
<li><code>var</code> is no type, but a placeholder</li>
<li><code>object</code> is the most general type; everything is an object</li>
<li><code>dynamic</code> is no type, but a switch</li>
<li>It tells the compiler to use the DLR</li>
</ul>
</section>
</div></div>
<div class="slide" id="reflection"><div>
<section>
<header>
<h2>Reflection</h2>
</header>
<ul>
<li>Sometimes an instance of <code>Type</code> is required</li>
<li>We got that type with <code>typeof()</code> (at compile-time)</li>
<li>Or if we already had an instance: <code>GetType()</code></li>
<li>This <code>Type</code> object contains all information about the type</li>
<li>We can find out what properties (names and types) are defined</li>
<li>Also what methods and events are available can be found</li>
<li>Or what interfaces are implemented and what's the base class</li>
</ul>
</section>
</div></div>
<div class="slide" id="using-reflection"><div>
<section>
<header>
<h2>Using reflection</h2>
</header>
<img src="pictures/reflection.png" class="middle r" />
</section>
</div></div>
<div class="slide" id="example-reflection"><div>
<section>
<header>
<h2 class="example">→ Example - Reflection</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day7/Reflection" title="Download source" class="example-download">Reflection.zip</a>
</section>
</div></div>
<div class="slide" id="presentations"><div>
<section>
<header>
<h2>All available presentations</h2>
</header>
<div class="left">
<b>Week 1</b>
<ul>
<li><a href="day1.html">Presentation of Monday</a></li>
<li><a href="day2.html">Presentation of Tuesday</a></li>
<li><a href="day3.html">Presentation of Wednesday</a></li>
<li><a href="day4.html">Presentation of Thursday</a></li>
<li><a href="day5.html">Presentation of Friday</a></li>
</ul>
</div>
<div class="right">
<b>Week 2</b>
<ul>
<li><a href="day6.html">Presentation of Monday</a></li>
<li><a href="day7.html">Presentation of Tuesday</a></li>
<li><a href="day8.html">Presentation of Wednesday</a></li>
<li><a href="day9.html">Presentation of Thursday</a></li>
</ul>
</div>
</section>
</div></div>
<div class="progress"><div></div></div>
<script src="scripts/script.js"></script>
<!-- Copyright © 2013 Florian Rappl, www.florian-rappl.de -->
</body>
</html>