This documentation is archived and is not being maintained.

How to: Apply a Transform to a BitmapImage

This example demonstrates how to apply a Transform to a BitmapImage.

///// Create a BitmapImage and set it's DecodePixelWidth to 200. Use  ///// 
///// this BitmapImage as a source for other BitmapSource objects.    /////

BitmapImage myBitmapImage = new BitmapImage();

// BitmapSource objects like BitmapImage can only have their properties 
// changed within a BeginInit/EndInit block.
myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");

// To save significant application memory, set the DecodePixelWidth or   
// DecodePixelHeight of the BitmapImage value of the image source to the desired  
// height or width of the rendered image. If you don't do this, the application will  
// cache the image as though it were rendered as its normal size rather then just  
// the size that is displayed. 
// Note: In order to preserve aspect ratio, set DecodePixelWidth 
// or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200;

/////////////////// Create a BitmapSource that Rotates the image ////////////////////// 
// Use the BitmapImage created above as the source for a new BitmapSource object 
// that will be scaled to a different size. Create a new BitmapSource by    
// scaling the original one.                                                
// Note: New BitmapSource does not cache. It is always pulled when required. 

// Create the new BitmapSource that will be used to scale the size of the source.
TransformedBitmap myRotatedBitmapSource = new TransformedBitmap();

// BitmapSource objects like TransformedBitmap can only have their properties 
// changed within a BeginInit/EndInit block.

// Use the BitmapSource object defined above as the source for this BitmapSource. 
// This creates a "chain" of BitmapSource objects which essentially inherit from each other.
myRotatedBitmapSource.Source = myBitmapImage;

// Flip the source 90 degrees.
myRotatedBitmapSource.Transform = new RotateTransform(90);