Page 1 of 1

Downloading and displaying images in XNA Downloading and displaying images in XNA

#1 hawksprite  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 11-September 09

Posted 26 January 2010 - 05:19 PM

One question i get asked allot by people trying to develop games that have multi player functionality and user created content, is how to display an image that isn't already on the local machine. In this tutorial i'll try my best to show you different methods for doing this. I'll start simple.

To start we'll make a simple XNA game that downloads an image as a Texture2D and displays that using sprite batch.

The first thing we need to add is all the components we'll need.
		GraphicsDeviceManager graphics;
		SpriteBatch spriteBatch;


		Texture2D backgroundTexture;


All were doing is adding a 2D Texture, the one were gonna download.

*NOTE you need to add the using System.Net; to the using statements for this to work.

Now we need to download the file, we'll do that in the LoadContent void.
			spriteBatch = new SpriteBatch(GraphicsDevice);

			string fileName = "logo.jpg";
			string server = "http://hawksprite.com/logo/" + fileName;

			WebClient Client = new WebClient();

			Client.DownloadFile(server, "Content/" + fileName);

		   backgroundTexture = Texture2D.FromFile(graphics.GraphicsDevice, @"Content/" + fileName);


What this is doing is specifying where to download the image, and what to download. Then it creates a WebClient to download the file, uses that client to download it to your "Content" Folder. And from there loads that downloaded image into your backgroundTexture, to later be displayed.

Now all thats left is an update to the Draw Void
		   GraphicsDevice.Clear(Color.CornflowerBlue);
			spriteBatch.Begin();
			spriteBatch.Draw(backgroundTexture, Vector2.Zero, Color.White);
			spriteBatch.End();
			base.Draw(gameTime);


This is fairly simple were just useing a spriteBatch to draw the backgroundTexture at (0,0).

And if you build it, it should work fine (=
This can be used for alot of other things to, you could have all your textures be downloaded in the begging of the game, so after its released you can still change how the game looks without the need of updates.

Screen shot of what it should look like:
Posted Image

Complete source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Download_Example
{

	public class Game1 : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;
		SpriteBatch spriteBatch;


		Texture2D backgroundTexture;

		public Game1()
		{
			graphics = new GraphicsDeviceManager(this);
			Content.RootDirectory = "Content";
		}


		protected override void Initialize()
		{


			base.Initialize();
		}

		protected override void LoadContent()
		{

			spriteBatch = new SpriteBatch(GraphicsDevice);
			string fileName = "logo.jpg";
			string server = "http://hawksprite.com/logo/" + fileName;

			WebClient Client = new WebClient();

			Client.DownloadFile(server, "Content/" + fileName);

			backgroundTexture = Texture2D.FromFile(graphics.GraphicsDevice, @"Content/" + fileName);
 
		}


		protected override void UnloadContent()
		{

		}


		protected override void Update(GameTime gameTime)
		{
			if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
				this.Exit();

			base.Update(gameTime);
		}


		protected override void Draw(GameTime gameTime)
		{
			GraphicsDevice.Clear(Color.CornflowerBlue);
			spriteBatch.Begin();
			spriteBatch.Draw(backgroundTexture, Vector2.Zero, Color.White);
			spriteBatch.End();
			base.Draw(gameTime);
		}
	}
}




Is This A Good Question/Topic? 0
  • +

Page 1 of 1