title: Add TransformGroup to a FramworkElement when rendering WPF to a PNG
body: <p>I've got an app that turns some XAML Usercontrols into PNGs - this has worked really well up to now, unfortunately I now need to double the size of the images.</p>
  
  <p>My method (that doesn't work!) was to add a ScaleTransform to the visual element after I've loaded it ... </p>
  
  <p>This line is the new line at the top of the SaveUsingEncoder method.</p>
  
  <p>visual.RenderTransform = GetScaleTransform(2);</p>
  
  <p>The PNG is the new size (3000 x 2000) - but the XAML is Rendered at 1500x1000 in the centre of the image.</p>
  
  <p>Can anyone assist please?</p>
  
  <pre><code>    private void Load(string filename)
      {
          var stream = new FileStream(filename), FileMode.Open);
          var frameworkElement = (FrameworkElement)(XamlReader.Load(stream));
  
          var scale = 2;
  
          var encoder = new PngBitmapEncoder();
          var availableSize = new Size(1500 * scale, 1000 * scale);
  
          frameworkElement.Measure(availableSize);
          frameworkElement.Arrange(new Rect(availableSize));
          name = name.Replace(" ", "-");
          SaveUsingEncoder(frameworkElement, string.Format(@"{0}.png", name), encoder, availableSize);
      }
  
      private TransformGroup GetScaleTransform(int scale)
      {
          var myScaleTransform = new ScaleTransform {ScaleY = scale, ScaleX = scale};
          var myTransformGroup = new TransformGroup();
          myTransformGroup.Children.Add(myScaleTransform);
          return myTransformGroup;
      }
  
      private void SaveUsingEncoder(FrameworkElement visual, string fileName, BitmapEncoder encoder, Size size)
      {
          visual.RenderTransform = GetScaleTransform(2);
          var bitmap = new RenderTargetBitmap(
              (int) size.Width,
              (int) size.Height,
              96,
              96,
              PixelFormats.Pbgra32);
          bitmap.Render(visual);
  
          var frame = BitmapFrame.Create(bitmap);
          encoder.Frames.Add(frame);
  
          using (var stream = File.Create(fileName))
          {
              encoder.Save(stream);
          }
      }
  </code></pre>