Programming4us
         
 
 
Windows Phone

Windows Phone 7 : Images Via the Web

2/10/2011 5:09:48 PM
One feature that’s really nice about the Image element is that you can set the Source property to a URL, such as in this Silverlight project:
Example 1. Silverlight Project: SilverlightWebBitmap File: MainPage.xaml (excerpt)
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image Source="http://www.charlespetzold.com/Media/HelloWP7.jpg" />
</Grid>

Here it is:



This is certainly easy enough, and pulling images off the Web rather than binding them into the application certainly keeps the size of the executable down. But an application running on Windows Phone 7 is not guaranteed to have an Internet connection, and you’re undoubtedly associated with other problems associated with downloading. The Image element has two events named ImageOpened and ImageFailed that you can use to determine if the download was successful or not.

For Windows Phone 7 programs that display a lot of bitmaps, you need to do some hard thinking. You can embed the bitmaps into the executable and have their access guaranteed, or you can save space and download them when necessary.

In XNA, downloading a bitmap from the Web is not quite as easy, but a .NET class named WebClient makes the job relatively painless. It’s somewhat easier to use than the common alternative (HttpWebRequest and HttpWebResponse) and is often the preferred choice for downloading individual items.

You can use WebClient to download either strings (commonly XML files) or binary objects. The actual transfer occurs asynchronously and then WebClient calls a method in your program to indicate completion or failure. This method call is in your program’s thread, so you get the benefit of an asynchronous data transfer without explicitly dealing with secondary threads.

To use WebClient in an XNA program, you’ll need to add a reference to the System.Net library: In the Solution Explorer, under the project name, right click References and select Add Reference. In the .NET table, select System.Net. (Silverlight programs get a reference to System.Net automatically.)

The Game1.cs file of the XnaWebBitmap project also requires a using directive for the System.Net namespace. The program defines the same fields as the earlier program:

Example 2. XNA Project: XnaWebBitmap File: Game1.cs (excerpt showing fields)
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D helloTexture;
Vector2 position;
. . .
}

The LoadContent method creates an instance of WebClient, sets the callback method, and then initiates the transfer:

Example 3. XNA Project: XnaWebBitmap File: Game1.cs (excerpt)
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);

WebClient webClient = new WebClient();
webClient.OpenReadCompleted += OnWebClientOpenReadCompleted;
webClient.OpenReadAsync(new Uri("http://www.charlespetzold.com/Media/HelloWP7.
jpg"));
}


The OnWebClientOpenReadCompleted method is called when the entire file has been downloaded. You’ll want to check if the download hasn’t been cancelled and that no error has been reported. If everything is OK, the Result property of the event arguments is of type Stream. You can use that Stream with the static Texture2D.FromStream method to create a Texture2D object:

Example 4. XNA Project: XnaWebBitmap File: Game1.cs (excerpt)
void OnWebClientOpenReadCompleted(object sender, OpenReadCompletedEventArgs args)
{
if (!args.Cancelled && args.Error == null)
{
helloTexture = Texture2D.FromStream(this.GraphicsDevice, args.Result);
Viewport viewport = this.GraphicsDevice.Viewport;
position = new Vector2((viewport.Width - helloTexture.Width) / 2,
(viewport.Height - helloTexture.Height) / 2);
}
}


The Texture2D.FromStream method supports JPEG, PNG, and GIF.

By default, the AllowReadStreamBuffering property of WebClient is true, which means that the entire file will have been downloaded when the OpenReadCompleted event is raised. The Stream object available in the Result property is actually a memory stream, except that it’s an instance of a class internal to the .NET libraries rather than MemoryStream itself.

If you set AllowReadStreamBuffering to false, then the Result property will be a network stream. The Texture2D class will not allow you to read from that stream on the main program thread.

Normally the LoadContent method of a Game derivative is called before the first call to the Update or Draw method, but it is essential to remember that a gap of time will separate LoadContent from the OnWebClientOpenReadCompleted method. During that time an asynchronous read is occurring, but the Game1 class is proceeding as normal with calls to Update and Draw. For that reason, you should only attempt to access the Texture2D object when you know that it’s valid:

Example 5. XNA Project: XnaWebBitmap File: Game1.cs (excerpt)
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Navy);

if (helloTexture != null)
{
spriteBatch.Begin();
spriteBatch.Draw(helloTexture, position, Color.White);
spriteBatch.End();
}

base.Draw(gameTime);
}

In a real program, you’d also want to provide some kind of notification to the user if the bitmap could not be downloaded.

Other -----------------
- Windows Phone 7 : Customizing Your E-Mail Signature
- Windows Phone 7 : Managing Mail Folders
- Windows Phone 7: The Silverlight Image Element
- Windows Phone 7: XNA Texture Drawing
- Windows Phone 7: An Introduction to Touch - Routed Events
- Windows Phone 7 : Working with Attachments
- Programming Windows Phone 7: An Introduction to Touch - The Manipulation Events
- Programming Windows Phone 7: An Introduction to Touch - Low-Level Touch Events in Silverlight
- Windows Phone 7: Composing a New Message
- Programming Windows Phone 7: An Introduction to Touch - The XNA Gesture Interface
- Programming Windows Phone 7: An Introduction to Touch - Low-Level Touch Handling in XNA
- Windows Phone 7: Responding to a Message
- Windows Phone 7: Checking for New Messages
- Windows Phone 7: Sorting and Searching Your Mail
- Windows Phone 7: Customizing Your Contacts List
- Windows Phone 7: Working with the Me Card
- Windows Phone 7: Posting to Facebook or Windows Live
- Programming Windows Phone 7 : Simple Clocks (part 2)
- Programming Windows Phone 7 : Simple Clocks (part 1)
- Windows Phone7: Pinning a Contact to Start
 
 
Most View
- Windows Server 2008 : Installing the Web Server Role (part 1)
- Windows 7 : Displaying the Router’s Setup Pages
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 2)
- Windows 7: Troubleshooting Tools (part 1) - Running the Windows 7 Troubleshooters
- Developing for Windows Phone and Xbox Live : What Is in a New Project?
- Microsoft Dynamics GP 2010 : Installing the Dynamics GP 2010 application
- Exchange Server 2010 : Availability Planning for Client Access Servers (part 4) - Creating a Client Access Server Array
- Scripting Windows 7 with WSH : Programming the WshShell Object (part 2)
- SharePoint 2010 : SQL Server Reporting Services 2008 (part 1)
- Windows 7 : Configuring Hardware and Applications - Managing Printers
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS