Visual Capture Util : UI Element « Windows Presentation Foundation « C# / C Sharp






Visual Capture Util

        

// (c) Copyright Cory Plotts.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.


using System;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows;

namespace Snoop
{
  class VisualCaptureUtil
  {
    public static void SaveVisual(Visual visual, int dpi, string filename)
    {
      // sometimes RenderTargetBitmap doesn't render the Visual or doesn't render the Visual properly
      // below i am using the trick that jamie rodriguez posted on his blog
      // where he wraps the Visual inside of a VisualBrush and then renders it.
      // http://blogs.msdn.com/b/jaimer/archive/2009/07/03/rendertargetbitmap-tips.aspx

      if (visual == null)
        return;

      Rect bounds;
      UIElement uiElement = visual as UIElement;
      if (uiElement != null)
      {
        bounds = new Rect(new Size((int)uiElement.RenderSize.Width, (int)uiElement.RenderSize.Height));
      }
      else
      {
        bounds = VisualTreeHelper.GetDescendantBounds(visual);
      }

      double sizeFactor = dpi / BaseDpi;
      RenderTargetBitmap rtb =
        new RenderTargetBitmap
        (
          (int)(bounds.Width * sizeFactor),
          (int)(bounds.Height * sizeFactor),
          dpi,
          dpi,
          PixelFormats.Pbgra32
        );

      DrawingVisual dv = new DrawingVisual();
      using (DrawingContext ctx = dv.RenderOpen())
      {
        VisualBrush vb = new VisualBrush(visual);
        ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
      }
      rtb.Render(dv);

      SaveRTBAsPNG(rtb, filename);
    }

    private static void SaveRTBAsPNG(RenderTargetBitmap bitmap, string filename)
    {
      var pngBitmapEncoder = new System.Windows.Media.Imaging.PngBitmapEncoder();
      pngBitmapEncoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmap));

      using (var fileStream = System.IO.File.Create(filename))
        pngBitmapEncoder.Save(fileStream);
    }

    private const double BaseDpi = 96;
  }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Parent and child timelines with default HoldEnd FillBehaviorParent and child timelines with default HoldEnd FillBehavior
2.Freezables and elementFreezables and element
3.Define a resource at the page root level on a FrameworkElement and reference itDefine a resource at the page root level on a FrameworkElement and reference it
4.Print Logical TreePrint Logical Tree
5.Print Visual TreePrint Visual Tree
6.Find enclosure componentFind enclosure component
7.Logical Visual Tree SampleLogical Visual Tree Sample
8.About Dialog with Tree WalkingAbout Dialog with Tree Walking
9.Point Hit Test with VisualTreeHelper.HitTestPoint Hit Test with VisualTreeHelper.HitTest
10.Find inner visual elementFind inner visual element
11.Print Visual(Canvas)Print Visual(Canvas)
12.Change control background in mouse enter and leaveChange control background in mouse enter and leave
13.Changing graphical elementsChanging graphical elements
14.Change the Visibility property of a UIElement.Change the Visibility property of a UIElement.
15.Change FillChange Fill
16.Update Focused Field for FrameworkElement
17.Find Visual Child
18.Is Logical Ancestor Of