The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Embedding Resources within an Avalon Application

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Tim Sneath

Posts: 395
Nickname: timsneath
Registered: Aug, 2003

Tim Sneath is a .NET developer for Microsoft in the UK.
Embedding Resources within an Avalon Application Posted: Jan 12, 2005 12:07 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Tim Sneath.
Original Post: Embedding Resources within an Avalon Application
Feed Title: Tim Sneath's Blog
Feed URL: /msdnerror.htm?aspxerrorpath=/tims/Rss.aspx
Feed Description: Random mumblings on Microsoft, .NET, and other topics.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Tim Sneath
Latest Posts From Tim Sneath's Blog

Advertisement

I've been writing a game recently using Avalon, as part of which I wanted to load and display a number of sprites and other graphical objects. There were two specific problems I wanted to resolve: what was the best way to cache the sprite images for efficient reuse, and how should I store and load the images themselves? In both cases, WinFX provided quite a neat solution that I wanted to share here.

To start with the problem of storing and loading images, the most straightforward approach would have been to simply load images from a bitmap file. Of course, then you have to deploy a series of image files with your application, which can become painful. Instead, Visual Studio allows you to embed resources directly into the application assembly, allowing you to distribute the executable code and any dependent resources as a single file. To achieve this, you simply have to add the bitmap images into your project and mark them as "Embedded Resources" within the content property (use F4 to bring up the properties window if it's not currently active). The resources then get compiled into the application itself, which you can see if you inspect the assembly with ILDASM. How do you access the bitmap image itself? It's a little opaque, but you use a command such as the following:

 stream = this.GetType().Assembly.GetManifestResourceStream(imageFilename);

where imageFilename is the full filename that was embedded (e.g. "background.bmp"). You can now create an instance of BitmapImage based on this stream and use it for whatever you want: for example, you could create an ImageBrush and use it to paint the background of an element.

The second half of the problem reflects that the game I'm working on has an avatar moving around a grid of tiles. On each frame, many of the tiles need repainting, which involves loading the images again. Some kind of image cache is obviously the solution to ensuring that this gets handled with reasonable performance. Fortunately the BitmapImage class has a constructor parameter that allows exactly that:

 public BitmapImage( Stream bitmapStream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption );

where BitmapCacheOption can be one of OnLoad, OnDemand or None. OnLoad is actually the default, so you get this caching goodness for free unless you explicitly unset it.

So here's the working code snippet to grab an image from an assembly resource, with error checking:

   // using System.Windows.Media;
   stream = this.GetType().Assembly.GetManifestResourceStream("background.bmp");
   if (stream != null)
   {
      BitmapImage bitmapImage = new System.Windows.Media.BitmapImage(
             stream,
             BitmapCreateOptions.PreservePixelFormat,
             BitmapCacheOption.OnLoad);
      if (bitmapImage != null)
      {
         ImageBrush ib = new ImageBrush(bitmapImage);
         ib.TileMode = TileMode.Tile;
         ib.Stretch = Stretch.None;
         GameArea.Background = ib;
         stream.Close();
      }
      else
      {
         throw new NullReferenceException("Stream had something in it, but it wasn't a bitmap.");
      }
   }
   else
   {
      throw new NullReferenceException("Stream is null - resource missing.");
   }

Incidentally, the embedded resources capability has no dependency on Avalon - you can do it just as well from a WinForms solution. Hope that helps someone. More tomorrow...

Read: Embedding Resources within an Avalon Application

Topic: New Toshiba Tablet Previous Topic   Next Topic Topic: Long time no write

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use