Share via


How to: Chain BitmapSource Objects Together

This example shows how you can apply a variety of effects to an image source by chaining multiple BitmapSource derived objects together.

The following example uses chaining to flip and change the pixel format of the source of an image.

Example

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">

  <Page.Resources>

    <!-- This resource defines a BitmapImage with a source and a
         DecodePixelWidth of 200. This property is set to the same value 
         as the desired width of the image to save on memory use. This 
         BitmapImage is used as the base for the other BitmapSource resources. -->
    <BitmapImage x:Key="masterImage" DecodePixelWidth="200" 
     UriSource="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Forest.jpg"/>

    <!-- This TransformedBitmap uses the BitmapImage defined above and flips
         it 90 degrees. -->
    <TransformedBitmap x:Key="rotatedImage" Source="{StaticResource masterImage}">
      <TransformedBitmap.Transform>
        <RotateTransform Angle="90" />
      </TransformedBitmap.Transform>
    </TransformedBitmap>

    <!-- This FormatConvertedBitmap uses the TransformedBitmap defined above and
         changes the format to Gray32Float (grayscale). -->
    <FormatConvertedBitmap x:Key="convertFormatImage" 
                           Source="{StaticResource rotatedImage}"
                           DestinationFormat="Gray32Float" />

  </Page.Resources>

  <StackPanel>

    <!-- Apply the "convertFormatImage" resource defined above to this image. 
         The resource is  a combination of all three BitmapSource objects which
         renders this image with DecodePixelWidth set to 200, fliped 90 degrees 
         and converted to grayscale. -->
    <Image Width="200" Source="{StaticResource convertFormatImage}" />
  </StackPanel>
</Page>
'This is a list of commonly used namespaces for a pane. 
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Imaging

Namespace SDKSample

    Class ChainedBitmapSourcesExample
        Inherits Page

        Public Sub New()
            '/// Create a BitmapImage and set it's DecodePixelWidth to 200. Use  ///// 
            '/// this BitmapImage as a source for other BitmapSource objects.    ///// 
            Dim myBitmapImage As New BitmapImage()

            ' BitmapSource objects like BitmapImage can only have their properties 
            ' changed within a BeginInit/EndInit block.
            myBitmapImage.BeginInit()
            myBitmapImage.UriSource = New Uri("sampleImages/WaterLilies.jpg", UriKind.Relative)

            ' 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
            myBitmapImage.EndInit()

            '///////////////// 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. 
            Dim myRotatedBitmapSource As New TransformedBitmap()

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

            ' 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
            ' Multiply the size of the X and Y axis of the source by 3.
            myRotatedBitmapSource.Transform = New RotateTransform(90)
            myRotatedBitmapSource.EndInit()

            ' Create a new BitmapSource using a different format than the original one. 
            Dim newFormatedBitmapSource As New FormatConvertedBitmap()

            ' BitmapSource objects like FormatConvertedBitmap can only have their properties 
            ' changed within a BeginInit/EndInit block.
            newFormatedBitmapSource.BeginInit()
            newFormatedBitmapSource.Source = myRotatedBitmapSource
            newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray32Float
            newFormatedBitmapSource.EndInit()

            ' Create Image Element 
            Dim myImage As New Image()
            myImage.Width = 200
            'set image source
            myImage.Source = newFormatedBitmapSource

            ' Add Image to the UI 
            Dim myStackPanel As New StackPanel()
            myStackPanel.Children.Add(myImage)
            Me.Content = myStackPanel

        End Sub 'New 


        Public Sub PageLoaded(ByVal sender As Object, ByVal args As RoutedEventArgs)

        End Sub 'PageLoaded
    End Class 'ChainedBitmapSourcesExample 
End Namespace
//This is a list of commonly used namespaces for a pane. 
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;


namespace SDKSample
{
    public partial class ChainedBitmapSourcesExample : Page
    {
        public ChainedBitmapSourcesExample()
        {
            ///// 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.BeginInit();
            myBitmapImage.UriSource = new Uri("sampleImages/WaterLilies.jpg",UriKind.Relative);

            // 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;
            myBitmapImage.EndInit();

            /////////////////// 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.
            myRotatedBitmapSource.BeginInit();

            // 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;
            // Multiply the size of the X and Y axis of the source by 3.
            myRotatedBitmapSource.Transform = new RotateTransform(90);
            myRotatedBitmapSource.EndInit();

            // Create a new BitmapSource using a different format than the original one.
            FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();

            // BitmapSource objects like FormatConvertedBitmap can only have their properties 
            // changed within a BeginInit/EndInit block.
            newFormatedBitmapSource.BeginInit();
            newFormatedBitmapSource.Source = myRotatedBitmapSource;
            newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
            newFormatedBitmapSource.EndInit();

            // Create Image Element
            Image myImage = new Image();
            myImage.Width = 200;
            //set image source
            myImage.Source = newFormatedBitmapSource;

            // Add Image to the UI
            StackPanel myStackPanel = new StackPanel();
            myStackPanel.Children.Add(myImage);
            this.Content = myStackPanel;
        }

        public void PageLoaded(object sender, RoutedEventArgs args)
        {


        }

    }
}