Skip to content

Commit dca268e

Browse files
committed
Modernize c# snippets
1 parent ac69dc2 commit dca268e

38 files changed

Lines changed: 2635 additions & 2655 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
internal static class Program
4+
{
5+
[STAThread]
6+
private static void Main()
7+
{
8+
BitmapConstructorForm1.Run();
9+
BitmapConstructorForm11.Run();
10+
BitmapConstructorForm12.Run();
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0-windows</TargetFramework>
6+
<UseWindowsForms>true</UseWindowsForms>
7+
</PropertyGroup>
8+
9+
</Project>

snippets/csharp/System.Drawing/Bitmap/.ctor/form1.cs

Lines changed: 70 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
using System.Drawing;
33
using System.Windows.Forms;
44

5-
public class Form1:
5+
public class BitmapConstructorForm1 :
66
System.Windows.Forms.Form
77

88
{
99
#region " Windows Form Designer generated code "
1010

11-
public Form1() : base()
12-
{
11+
public BitmapConstructorForm1() : base()
12+
{
1313

1414
//This call is required by the Windows Form Designer.
1515
InitializeComponent();
16-
this.Button1.Click += new EventHandler(Button1_Click);
17-
this.Button2.Click += new EventHandler(Button2_Click);
16+
Button1.Click += new EventHandler(Button1_Click);
17+
Button2.Click += new EventHandler(Button2_Click);
1818
InitializeBitmap();
1919
InitializeStreamBitmap();
2020

@@ -26,16 +26,13 @@ protected override void Dispose(bool disposing)
2626
{
2727
if (disposing)
2828
{
29-
if (components != null)
30-
{
31-
components.Dispose();
32-
}
29+
components?.Dispose();
3330
}
3431
base.Dispose(disposing);
3532
}
3633

3734
//Required by the Windows Form Designer
38-
private System.ComponentModel.IContainer components;
35+
private System.ComponentModel.IContainer components = null;
3936

4037
//NOTE: The following procedure is required by the Windows Form Designer
4138
//It can be modified using the Windows Form Designer.
@@ -47,43 +44,43 @@ protected override void Dispose(bool disposing)
4744
[System.Diagnostics.DebuggerStepThrough]
4845
private void InitializeComponent()
4946
{
50-
this.Button1 = new System.Windows.Forms.Button();
51-
this.PictureBox1 = new System.Windows.Forms.PictureBox();
52-
this.Button2 = new System.Windows.Forms.Button();
53-
this.SuspendLayout();
47+
Button1 = new System.Windows.Forms.Button();
48+
PictureBox1 = new System.Windows.Forms.PictureBox();
49+
Button2 = new System.Windows.Forms.Button();
50+
SuspendLayout();
5451
//
5552
// Button1
5653
//
57-
this.Button1.Location = new System.Drawing.Point(24, 192);
58-
this.Button1.Name = "Button1";
59-
this.Button1.Size = new System.Drawing.Size(96, 23);
60-
this.Button1.TabIndex = 2;
61-
this.Button1.Text = "Rotate and Flip";
54+
Button1.Location = new System.Drawing.Point(24, 192);
55+
Button1.Name = "Button1";
56+
Button1.Size = new System.Drawing.Size(96, 23);
57+
Button1.TabIndex = 2;
58+
Button1.Text = "Rotate and Flip";
6259
//
6360
// PictureBox1
6461
//
65-
this.PictureBox1.Location = new System.Drawing.Point(48, 40);
66-
this.PictureBox1.Name = "PictureBox1";
67-
this.PictureBox1.Size = new System.Drawing.Size(168, 72);
68-
this.PictureBox1.TabIndex = 3;
69-
this.PictureBox1.TabStop = false;
62+
PictureBox1.Location = new System.Drawing.Point(48, 40);
63+
PictureBox1.Name = "PictureBox1";
64+
PictureBox1.Size = new System.Drawing.Size(168, 72);
65+
PictureBox1.TabIndex = 3;
66+
PictureBox1.TabStop = false;
7067
//
7168
// Button2
7269
//
73-
this.Button2.Location = new System.Drawing.Point(152, 192);
74-
this.Button2.Name = "Button2";
75-
this.Button2.TabIndex = 4;
76-
this.Button2.Text = "Button2";
70+
Button2.Location = new System.Drawing.Point(152, 192);
71+
Button2.Name = "Button2";
72+
Button2.TabIndex = 4;
73+
Button2.Text = "Button2";
7774
//
7875
// Form1
7976
//
80-
this.ClientSize = new System.Drawing.Size(292, 266);
81-
this.Controls.Add(this.Button2);
82-
this.Controls.Add(this.PictureBox1);
83-
this.Controls.Add(this.Button1);
84-
this.Name = "Form1";
85-
this.Text = "Form1";
86-
this.ResumeLayout(false);
77+
ClientSize = new System.Drawing.Size(292, 266);
78+
Controls.Add(Button2);
79+
Controls.Add(PictureBox1);
80+
Controls.Add(Button1);
81+
Name = "Form1";
82+
Text = "Form1";
83+
ResumeLayout(false);
8784
}
8885

8986
#endregion
@@ -120,20 +117,20 @@ private void InitializeBitmap()
120117
{
121118
try
122119
{
123-
bitmap1 = (Bitmap)Bitmap.FromFile(@"C:\Documents and Settings\" +
120+
bitmap1 = (Bitmap)Bitmap.FromFile(@"C:\Documents and Settings\" +
124121
@"All Users\Documents\My Music\music.bmp");
125122
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
126123
PictureBox1.Image = bitmap1;
127124
}
128-
catch(System.IO.FileNotFoundException)
125+
catch (System.IO.FileNotFoundException)
129126
{
130-
MessageBox.Show("There was an error." +
127+
MessageBox.Show("There was an error." +
131128
"Check the path to the bitmap.");
132129
}
133130
}
134131
//</snippet4>
135132

136-
private void Button1_Click(System.Object sender, System.EventArgs e)
133+
private void Button1_Click(object sender, System.EventArgs e)
137134
{
138135

139136
if (bitmap1 != null)
@@ -153,11 +150,11 @@ private void Button1_Click(System.Object sender, System.EventArgs e)
153150
// a form that contains a button named Button2. Paste the code into the
154151
// form and associate this method with the button's Click event.
155152
//<snippet1>
156-
private void Button2_Click(System.Object sender, System.EventArgs e)
153+
private void Button2_Click(object sender, System.EventArgs e)
157154
{
158155

159156
Bitmap bitmap1 = Bitmap.FromHicon(SystemIcons.Hand.Handle);
160-
Graphics formGraphics = this.CreateGraphics();
157+
Graphics formGraphics = CreateGraphics();
161158
GraphicsUnit units = GraphicsUnit.Point;
162159

163160
RectangleF bmpRectangleF = bitmap1.GetBounds(ref units);
@@ -179,23 +176,23 @@ private void InitializeStreamBitmap()
179176
{
180177
try
181178
{
182-
System.Net.WebRequest request =
179+
System.Net.WebRequest request =
183180
System.Net.WebRequest.Create(
184181
"http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif");
185182
System.Net.WebResponse response = request.GetResponse();
186-
System.IO.Stream responseStream =
183+
System.IO.Stream responseStream =
187184
response.GetResponseStream();
188185
Bitmap bitmap2 = new Bitmap(responseStream);
189186
PictureBox1.Image = bitmap2;
190187
}
191-
catch(System.Net.WebException)
188+
catch (System.Net.WebException)
192189
{
193190
MessageBox.Show("There was an error opening the image file."
194191
+ "Check the URL");
195192
}
196193
}
197194
//</snippet2>
198-
// The following code example demonstrates how to use the Image.PixelFormat,
195+
// The following code example demonstrates how to use the Image.PixelFormat,
199196
// Image.Height, Image.Width, and BitmapData.Scan0 properties; the Bitmap.LockBits
200197
// and Bitmap.UnlockBits methods; and the ImageLockMode enumeration.
201198
// This example is designed to be used with Windows
@@ -204,45 +201,45 @@ private void InitializeStreamBitmap()
204201
// This example assumes the existence of an 24bpp image file named fakePhoto.jpg at c:\.
205202
//<snippet5>
206203
private void LockUnlockBitsExample(PaintEventArgs e)
207-
{
204+
{
208205

209-
// Create a new bitmap.
210-
Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
206+
// Create a new bitmap.
207+
Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
211208

212-
// Lock the bitmap's bits.
213-
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
214-
System.Drawing.Imaging.BitmapData bmpData =
215-
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
216-
bmp.PixelFormat);
209+
// Lock the bitmap's bits.
210+
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
211+
System.Drawing.Imaging.BitmapData bmpData =
212+
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
213+
bmp.PixelFormat);
217214

218-
// Get the address of the first line.
219-
IntPtr ptr = bmpData.Scan0;
215+
// Get the address of the first line.
216+
IntPtr ptr = bmpData.Scan0;
220217

221-
// Declare an array to hold the bytes of the bitmap.
222-
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
223-
byte[] rgbValues = new byte[bytes];
218+
// Declare an array to hold the bytes of the bitmap.
219+
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
220+
byte[] rgbValues = new byte[bytes];
224221

225-
// Copy the RGB values into the array.
226-
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
222+
// Copy the RGB values into the array.
223+
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
227224

228-
// Set every third value to 255. A 24bpp bitmap will look red.
229-
for (int counter = 2; counter < rgbValues.Length; counter += 3)
230-
rgbValues[counter] = 255;
225+
// Set every third value to 255. A 24bpp bitmap will look red.
226+
for (int counter = 2; counter < rgbValues.Length; counter += 3)
227+
rgbValues[counter] = 255;
231228

232-
// Copy the RGB values back to the bitmap
233-
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
229+
// Copy the RGB values back to the bitmap
230+
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
234231

235-
// Unlock the bits.
236-
bmp.UnlockBits(bmpData);
232+
// Unlock the bits.
233+
bmp.UnlockBits(bmpData);
234+
235+
// Draw the modified image.
236+
e.Graphics.DrawImage(bmp, 0, 150);
237+
}
237238

238-
// Draw the modified image.
239-
e.Graphics.DrawImage(bmp, 0, 150);
240-
}
241-
242239
//</snippet5>
243240

244-
public static void Main()
241+
public static void Run()
245242
{
246-
Application.Run(new Form1());
243+
Application.Run(new BitmapConstructorForm1());
247244
}
248245
}

snippets/csharp/System.Drawing/Bitmap/.ctor/form11.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
using System.Drawing;
33
using System.Windows.Forms;
44

5-
public class Form1:
5+
public class BitmapConstructorForm11 :
66
System.Windows.Forms.Form
77

88
{
99
#region " Windows Form Designer generated code "
1010

11-
public Form1() : base()
12-
{
11+
public BitmapConstructorForm11() : base()
12+
{
1313

1414
//This call is required by the Windows Form Designer.
1515
InitializeComponent();
@@ -22,16 +22,13 @@ protected override void Dispose(bool disposing)
2222
{
2323
if (disposing)
2424
{
25-
if (components != null)
26-
{
27-
components.Dispose();
28-
}
25+
components?.Dispose();
2926
}
3027
base.Dispose(disposing);
3128
}
3229

3330
//Required by the Windows Form Designer
34-
private System.ComponentModel.IContainer components;
31+
private System.ComponentModel.IContainer components = null;
3532

3633
//NOTE: The following procedure is required by the Windows Form Designer
3734
//It can be modified using the Windows Form Designer.
@@ -42,10 +39,10 @@ private void InitializeComponent()
4239
//
4340
// Form1
4441
//
45-
this.ClientSize = new System.Drawing.Size(292, 266);
46-
this.Name = "Form1";
47-
this.Text = "Form1";
48-
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
42+
ClientSize = new System.Drawing.Size(292, 266);
43+
Name = "Form1";
44+
Text = "Form1";
45+
Paint += new System.Windows.Forms.PaintEventHandler(Form1_Paint);
4946
}
5047

5148
#endregion
@@ -73,9 +70,9 @@ private void ConstructFromResourceSaveAsGif(PaintEventArgs e)
7370
}
7471
//</snippet1>
7572

76-
public static void Main()
73+
public static void Run()
7774
{
78-
Application.Run(new Form1());
75+
Application.Run(new BitmapConstructorForm11());
7976
}
8077

8178
private void Form1_Paint(object sender, PaintEventArgs e)

0 commit comments

Comments
 (0)