03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 20 ❘ 697<br />

}<br />

// Request the data stream.<br />

Uri uri = new Uri(urlTextBox.Text);<br />

Client = new WebClient();<br />

Client.OpenReadCompleted += Client_OpenReadCompleted;<br />

Client.OpenReadAsync(uri);<br />

// Cancel the request.<br />

private void cancelButton_Click(object sender, EventArgs e)<br />

{<br />

Client.CancelAsync();<br />

}<br />

// Process the stream.<br />

private void Client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)<br />

{<br />

// See what the result was.<br />

if (e.Error != null)<br />

{<br />

MessageBox.Show(e.Error.Message);<br />

}<br />

else if (e.Cancelled)<br />

{<br />

MessageBox.Show("Canceled");<br />

}<br />

else<br />

{<br />

// Get the data from the stream.<br />

// (You must close the stream when finished.)<br />

using (Stream stream = e.Result)<br />

{<br />

// Use the stream to create a Bitmap.<br />

Bitmap bitmap = new Bitmap(stream);<br />

}<br />

}<br />

// Display the result.<br />

imagePictureBox.Image = bitmap;<br />

}<br />

// Make the form big enough.<br />

Size size = new Size(<br />

Math.Max(imagePictureBox.Right + 12, ClientSize.Width),<br />

Math.Max(imagePictureBox.Bottom + 12, ClientSize.Height));<br />

ClientSize = size;<br />

7. The DownloadData example program uses the following code to do this.<br />

using (WebClient client = new WebClient())<br />

{<br />

byte[] bytes = client.DownloadData(urlTextBox.Text);<br />

// Make a stream associated with the bytes.<br />

using (MemoryStream stream = new MemoryStream(bytes))<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!