Game Development Reference
In-Depth Information
Loading Textures from a MemoryStream
The second source type for loading textures is from raw binary data stored in a
MemoryStream object. This type is useful when images are extracted from storage
archives, pulled off of a network connection, or programmatically generated. The
following code shows how to load an image into the texture browser control from
memory. Be sure to reference the System.Drawing and System.IO namespaces.
Image image = Image.FromFile(@”.\MemoryImage.bmp”);
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Png);
TextureBrowserInstance.AddTexture(new TextureHandle(“My Image”, stream));
Loading Textures from a Bitmap
The last source type provides the ability to use an existing bitmap image when cre-
ating a texture handle. This method does not require any additional loading, since
the image data has already been loaded into the bitmap object. This approach is
useful when retrieving embedded resource content from an assembly resource
stream, or images that are programmatically generated. The following code shows
how to load a texture from an existing bitmap.
Image image = Image.FromFile(@”.\MemoryImage.bmp”);
TextureBrowserInstance.AddTexture(new TextureHandle(“My Image”, image));
Texture Browser Demo
The Companion Web site has the complete source code for the library presented
in this chapter, along with a simple demo that utilizes it. The demo just loads images
located in a folder with the application, but supports the ability to switch image
loaders at runtime.
Multiple selection is enabled, and you just have to hold down the Ctrl key while
selecting multiple thumbnails. A context menu is bound to the right-click event of
the thumbnails to present a dialog that shows simple information about the image.
Lastly, the Activated event, (double-click), for the thumbnails shows a simple mes-
sage box where other functionality could be implemented in a real-world applica-
tion. An example of real-world functionality might be the ability to double-click a
thumbnail and have a selected model mesh reference the texture as its color map.
Figure 26.4 shows a screenshot of the texture browser demo application.
Search Nedrilad ::

Custom Search