title: WPF PngBitmapEncoder: how to disable background transparency?
body: <p>I am capturing a screenshot of a FrameworkElement and saving as a png file. However I would like to disable the background transparency for the png. How do I do that? </p>
  
  <p>I did not choose JpegBitmapEncoder because it doesn't render DropShadowEffect properly.
  The following is the code snippet</p>
  
  <pre><code>    private void EncodeAndSaveToFile(string fileName)
      {
          var bitmapSource = FrameworkElement.RenderToBitmap();
          if (bitmapSource != null)
          {
              using (var stream = new FileStream(fileName, FileMode.Create))
              {
                  var encoder = new PngBitmapEncoder();
                  encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
                  encoder.Save(stream);
              }
          }
      }
  
  
      public static BitmapSource RenderToBitmap(this UIElement element, double scale)
      {
  
          var renderWidth = (int)(element.RenderSize.Width * scale);
          var renderHeight = (int)(element.RenderSize.Height * scale);
  
          var renderTarget = new RenderTargetBitmap(renderWidth, renderHeight, 96, 96, PixelFormats.Pbgra32);
          var sourceBrush = new VisualBrush(element);
  
  
          var drawingVisual = new DrawingVisual();
          var drawingContext = drawingVisual.RenderOpen();
  
          using (drawingContext)
          {
              drawingContext.PushTransform(new ScaleTransform(scale, scale));
              drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(element.RenderSize.Width, element.RenderSize.Height)));
          }
          renderTarget.Render(drawingVisual);
  
          return renderTarget;
      }
  </code></pre>