How to create a Viewbox in WPF and C#

The following code sampple creates a Viewbox in C#. Create a WPF app in Visual Studio and copy this method and call it in the constructor of MainWindow. 
  1. private void CreateViewboxDynamically()    
  2. {    
  3.     // Create a Viewbox object    
  4.     Viewbox dynamicViewbox = new Viewbox();    
  5.     // Set StretchDirection and Stretch properties    
  6.     dynamicViewbox.StretchDirection = StretchDirection.Both;    
  7.     dynamicViewbox.Stretch = Stretch.Fill;    
  8.     dynamicViewbox.MaxWidth = 300;    
  9.     dynamicViewbox.MaxHeight = 200;    
  10.     
  11.     // Create an Ellipse dynamically    
  12.     Ellipse redCircle = new Ellipse();    
  13.     redCircle.Height = 100;    
  14.     redCircle.Width = 100;    
  15.     redCircle.Fill = new SolidColorBrush(Colors.Red);    
  16.     
  17.     // Set Viewbox.Child to Ellipse    
  18.     dynamicViewbox.Child = redCircle;    
  19.     
  20.     // Add Viewbox to Grid panel's child    
  21.     RootLayout.Children.Add(dynamicViewbox);    
  22. }    
Here is a detailed article: XAML Viewbox Tutorial