Skip to content

Commit 909293f

Browse files
committed
more updates
1 parent dca268e commit 909293f

5 files changed

Lines changed: 246 additions & 136 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ internal static class Program
55
[STAThread]
66
private static void Main()
77
{
8-
BitmapConstructorForm1.Run();
9-
BitmapConstructorForm11.Run();
8+
//BitmapConstructorForm1.Run();
9+
//BitmapConstructorForm11.Run();
1010
BitmapConstructorForm12.Run();
1111
}
1212
}

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

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

5-
public class BitmapConstructorForm1 :
6-
System.Windows.Forms.Form
5+
public class BitmapConstructorForm1 : Form
76

87
{
9-
#region " Windows Form Designer generated code "
10-
118
public BitmapConstructorForm1() : base()
129
{
13-
14-
//This call is required by the Windows Form Designer.
10+
// This call is required by the Windows Form Designer.
1511
InitializeComponent();
1612
Button1.Click += new EventHandler(Button1_Click);
1713
Button2.Click += new EventHandler(Button2_Click);
18-
InitializeBitmap();
14+
15+
string filePath = "path//to//your//image.bmp";
16+
InitializeBitmap(filePath);
1917
InitializeStreamBitmap();
2018

21-
//Add any initialization after the InitializeComponent() call
19+
// Add any initialization after the InitializeComponent() call.
2220
}
2321

24-
//Form overrides dispose to clean up the component list.
22+
// Form overrides Dispose to clean up the component list.
2523
protected override void Dispose(bool disposing)
2624
{
2725
if (disposing)
@@ -31,50 +29,50 @@ protected override void Dispose(bool disposing)
3129
base.Dispose(disposing);
3230
}
3331

34-
//Required by the Windows Form Designer
32+
// Required by the Windows Form Designer.
3533
private System.ComponentModel.IContainer components = null;
3634

37-
//NOTE: The following procedure is required by the Windows Form Designer
38-
//It can be modified using the Windows Form Designer.
39-
//Do not modify it using the code editor.
40-
internal System.Windows.Forms.Button Button1;
41-
internal System.Windows.Forms.PictureBox PictureBox1;
42-
internal System.Windows.Forms.Button Button2;
35+
// NOTE: The following procedure is required by the Windows Form Designer.
36+
// It can be modified using the Windows Form Designer.
37+
// Do not modify it using the code editor.
38+
internal Button Button1;
39+
internal PictureBox PictureBox1;
40+
internal Button Button2;
4341

4442
[System.Diagnostics.DebuggerStepThrough]
4543
private void InitializeComponent()
4644
{
47-
Button1 = new System.Windows.Forms.Button();
48-
PictureBox1 = new System.Windows.Forms.PictureBox();
49-
Button2 = new System.Windows.Forms.Button();
45+
Button1 = new Button();
46+
PictureBox1 = new PictureBox();
47+
Button2 = new Button();
5048
SuspendLayout();
5149
//
5250
// Button1
5351
//
54-
Button1.Location = new System.Drawing.Point(24, 192);
52+
Button1.Location = new Point(24, 192);
5553
Button1.Name = "Button1";
56-
Button1.Size = new System.Drawing.Size(96, 23);
54+
Button1.Size = new Size(96, 23);
5755
Button1.TabIndex = 2;
5856
Button1.Text = "Rotate and Flip";
5957
//
6058
// PictureBox1
6159
//
62-
PictureBox1.Location = new System.Drawing.Point(48, 40);
60+
PictureBox1.Location = new Point(48, 40);
6361
PictureBox1.Name = "PictureBox1";
64-
PictureBox1.Size = new System.Drawing.Size(168, 72);
62+
PictureBox1.Size = new Size(168, 72);
6563
PictureBox1.TabIndex = 3;
6664
PictureBox1.TabStop = false;
6765
//
6866
// Button2
6967
//
70-
Button2.Location = new System.Drawing.Point(152, 192);
68+
Button2.Location = new Point(152, 192);
7169
Button2.Name = "Button2";
7270
Button2.TabIndex = 4;
7371
Button2.Text = "Button2";
7472
//
7573
// Form1
7674
//
77-
ClientSize = new System.Drawing.Size(292, 266);
75+
ClientSize = new Size(292, 266);
7876
Controls.Add(Button2);
7977
Controls.Add(PictureBox1);
8078
Controls.Add(Button1);
@@ -83,8 +81,6 @@ private void InitializeComponent()
8381
ResumeLayout(false);
8482
}
8583

86-
#endregion
87-
8884
// <snippet3Intro>
8985
// The following code example demonstrates constructing how to construct
9086
// a new Bitmap from a file.
@@ -113,24 +109,22 @@ private void InitializeComponent()
113109
//<snippet4>
114110
Bitmap bitmap1;
115111

116-
private void InitializeBitmap()
112+
private void InitializeBitmap(string filePath)
117113
{
118114
try
119115
{
120-
bitmap1 = (Bitmap)Bitmap.FromFile(@"C:\Documents and Settings\" +
121-
@"All Users\Documents\My Music\music.bmp");
116+
bitmap1 = (Bitmap)Bitmap.FromFile(filePath);
122117
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
123118
PictureBox1.Image = bitmap1;
124119
}
125120
catch (System.IO.FileNotFoundException)
126121
{
127-
MessageBox.Show("There was an error." +
128-
"Check the path to the bitmap.");
122+
MessageBox.Show("There was an error. Check the path to the bitmap.");
129123
}
130124
}
131125
//</snippet4>
132126

133-
private void Button1_Click(object sender, System.EventArgs e)
127+
private void Button1_Click(object sender, EventArgs e)
134128
{
135129

136130
if (bitmap1 != null)
@@ -150,9 +144,8 @@ private void Button1_Click(object sender, System.EventArgs e)
150144
// a form that contains a button named Button2. Paste the code into the
151145
// form and associate this method with the button's Click event.
152146
//<snippet1>
153-
private void Button2_Click(object sender, System.EventArgs e)
147+
private void Button2_Click(object sender, EventArgs e)
154148
{
155-
156149
Bitmap bitmap1 = Bitmap.FromHicon(SystemIcons.Hand.Handle);
157150
Graphics formGraphics = CreateGraphics();
158151
GraphicsUnit units = GraphicsUnit.Point;
@@ -176,38 +169,42 @@ private void InitializeStreamBitmap()
176169
{
177170
try
178171
{
179-
System.Net.WebRequest request =
180-
System.Net.WebRequest.Create(
181-
"http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif");
182-
System.Net.WebResponse response = request.GetResponse();
183-
System.IO.Stream responseStream =
184-
response.GetResponseStream();
185-
Bitmap bitmap2 = new Bitmap(responseStream);
172+
using System.Net.Http.HttpClient client = new();
173+
using System.Net.Http.HttpResponseMessage response =
174+
client.GetAsync("http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif")
175+
.GetAwaiter()
176+
.GetResult();
177+
response.EnsureSuccessStatusCode();
178+
using System.IO.Stream responseStream =
179+
response.Content.ReadAsStreamAsync()
180+
.GetAwaiter()
181+
.GetResult();
182+
Bitmap bitmap2 = new(responseStream);
186183
PictureBox1.Image = bitmap2;
187184
}
188-
catch (System.Net.WebException)
185+
catch (System.Net.Http.HttpRequestException)
189186
{
190-
MessageBox.Show("There was an error opening the image file."
191-
+ "Check the URL");
187+
MessageBox.Show("There was an error opening the image file. Check the URL");
192188
}
193189
}
194190
//</snippet2>
191+
195192
// The following code example demonstrates how to use the Image.PixelFormat,
196193
// Image.Height, Image.Width, and BitmapData.Scan0 properties; the Bitmap.LockBits
197194
// and Bitmap.UnlockBits methods; and the ImageLockMode enumeration.
198195
// This example is designed to be used with Windows
199196
// Forms. To run this example, paste it into a form and handle the form's Paint event by
200197
// calling the LockUnlockBitsExample method, passing e as PaintEventArgs.
201198
// This example assumes the existence of an 24bpp image file named fakePhoto.jpg at c:\.
199+
202200
//<snippet5>
203-
private void LockUnlockBitsExample(PaintEventArgs e)
201+
private static void LockUnlockBitsExample(PaintEventArgs e)
204202
{
205-
206203
// Create a new bitmap.
207-
Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
204+
Bitmap bmp = new("c:\\fakePhoto.jpg");
208205

209206
// Lock the bitmap's bits.
210-
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
207+
Rectangle rect = new(0, 0, bmp.Width, bmp.Height);
211208
System.Drawing.Imaging.BitmapData bmpData =
212209
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
213210
bmp.PixelFormat);
@@ -224,7 +221,9 @@ private void LockUnlockBitsExample(PaintEventArgs e)
224221

225222
// Set every third value to 255. A 24bpp bitmap will look red.
226223
for (int counter = 2; counter < rgbValues.Length; counter += 3)
224+
{
227225
rgbValues[counter] = 255;
226+
}
228227

229228
// Copy the RGB values back to the bitmap
230229
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

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

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

5-
public class BitmapConstructorForm11 :
6-
System.Windows.Forms.Form
5+
public class BitmapConstructorForm11 : Form
76

87
{
9-
#region " Windows Form Designer generated code "
10-
118
public BitmapConstructorForm11() : base()
129
{
1310

14-
//This call is required by the Windows Form Designer.
11+
// This call is required by the Windows Form Designer.
1512
InitializeComponent();
1613

17-
//Add any initialization after the InitializeComponent() call
14+
// Add any initialization after the InitializeComponent() call.
1815
}
1916

20-
//Form overrides dispose to clean up the component list.
17+
// Form overrides Dispose to clean up the component list.
2118
protected override void Dispose(bool disposing)
2219
{
2320
if (disposing)
@@ -27,38 +24,45 @@ protected override void Dispose(bool disposing)
2724
base.Dispose(disposing);
2825
}
2926

30-
//Required by the Windows Form Designer
27+
// Required by the Windows Form Designer.
3128
private System.ComponentModel.IContainer components = null;
3229

33-
//NOTE: The following procedure is required by the Windows Form Designer
34-
//It can be modified using the Windows Form Designer.
35-
//Do not modify it using the code editor.
30+
// NOTE: The following procedure is required by the Windows Form Designer.
31+
// It can be modified using the Windows Form Designer.
32+
// Do not modify it using the code editor.
3633
[System.Diagnostics.DebuggerStepThrough]
3734
private void InitializeComponent()
3835
{
3936
//
4037
// Form1
4138
//
42-
ClientSize = new System.Drawing.Size(292, 266);
39+
ClientSize = new Size(292, 266);
4340
Name = "Form1";
4441
Text = "Form1";
45-
Paint += new System.Windows.Forms.PaintEventHandler(Form1_Paint);
42+
Paint += new PaintEventHandler(Form1_Paint);
4643
}
4744

48-
#endregion
49-
5045
//<snippet1>
51-
private void ConstructFromResourceSaveAsGif(PaintEventArgs e)
46+
private static void ConstructFromResourceSaveAsGif(PaintEventArgs e)
5247
{
53-
5448
// Construct a bitmap from the button image resource.
55-
Bitmap bmp1 = new Bitmap(typeof(Button), "Button.bmp");
49+
string resourceName = "Button.bmp";
50+
Bitmap bmp1;
51+
try
52+
{
53+
bmp1 = new(typeof(Button), resourceName);
54+
}
55+
catch (ArgumentException ex)
56+
{
57+
MessageBox.Show($"The resource '{resourceName}' was not found. {ex.Message}");
58+
return;
59+
}
5660

5761
// Save the image as a GIF.
5862
bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Gif);
5963

6064
// Construct a new image from the GIF file.
61-
Bitmap bmp2 = new Bitmap("c:\\button.gif");
65+
Bitmap bmp2 = new("c:\\button.gif");
6266

6367
// Draw the two images.
6468
e.Graphics.DrawImage(bmp1, new Point(10, 10));

0 commit comments

Comments
 (0)