using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace ClipTest{ ///<summary> /// Provides methods to copy a Control or UserControl to the Windows clipboard as a BMP ///</summary> public class CopyToClipBoard { private string _ErrorMessage = string.Empty; /// <summary> /// Public property, which stores any Exception message encountered during capture /// </summary> /// <remarks> /// If CopyControlToClipboard returns false, check this property /// </remarks> public string ErrorMessage { get { return _ErrorMessage; } set { ErrorMessage = _ErrorMessage; } } /// <summary> /// Allows copying of a Control's screen area to the clipboard as a BMP /// </summary> /// <param name="ControlToCapture"> /// A Control, which is captured to the clipboard entirely /// </param> /// <example> /// Calling this method to capture a Control /// <code> /// /// CopyToClipBoard ctc = new CopyToClipBoard(); /// bool bCopied; /// bCopied = ctc.CopyControlToClipboard(ControlName); /// /// </code> /// </example> /// <permission cref="System.Security.PermissionSet"> /// Everyone can access this method. /// </permission> public bool CopyControlToClipboard(Control ControlToCapture) { try { // Get dimensions of the Control we are to capture Size renderSize = ControlToCapture.RenderSize; // Create a RenderTargetBitmap of the same dimensions to store the capture RenderTargetBitmap rtb = new RenderTargetBitmap((int)renderSize.Width, (int)renderSize.Height, 96, 96, PixelFormats.Pbgra32); // Render a white background in Clipboard Rectangle vRect = new Rectangle() { Width = ControlToCapture.RenderSize.Width, Height = ControlToCapture.RenderSize.Height, Fill = Brushes.White }; vRect.Measure(renderSize); vRect.Arrange(new Rect(renderSize)); rtb.Render(vRect); // Grab the Control area to a RenderTargetBitmap ControlToCapture.Measure(renderSize); ControlToCapture.Arrange(new Rect(renderSize)); rtb.Render(ControlToCapture); // And finally, put it in the Clipboard Clipboard.SetImage(rtb); return true; } catch (Exception ex) { // Set the ErrorMessage property for caller to check after false return _ErrorMessage = ex.ToString(); return false; } } /// <summary> /// Allows copying of a UserControl's screen area to the clipboard as a BMP /// </summary> /// <param name="UserControlToCapture"> /// A UserControl, which is captured to the clipboard entirely /// </param> /// <example> /// Calling this method from within the UserControl to capture /// <code> /// /// CopyToClipBoard ctc = new CopyToClipBoard(); /// bool bCopied; /// bCopied = ctc.CopyControlToClipboard(this); /// /// </code> /// </example> /// <permission cref="System.Security.PermissionSet"> /// Everyone can access this method. /// </permission> public bool CopyControlToClipboard(UserControl UserControlToCapture) { try { // Get dimensions of the Control we are to capture Size renderSize = UserControlToCapture.RenderSize; // Create a RenderTargetBitmap of the same dimensions to store the capture RenderTargetBitmap rtb = new RenderTargetBitmap((int)renderSize.Width, (int)renderSize.Height, 96, 96, PixelFormats.Pbgra32); // Render a white background in Clipboard Rectangle vRect = new Rectangle() { Width = UserControlToCapture.RenderSize.Width, Height = UserControlToCapture.RenderSize.Height, Fill = Brushes.White }; vRect.Measure(renderSize); vRect.Arrange(new Rect(renderSize)); rtb.Render(vRect); // Grab the Control area to a RenderTargetBitmap UserControlToCapture.Measure(renderSize); UserControlToCapture.Arrange(new Rect(renderSize)); rtb.Render(UserControlToCapture); // And finally, put it in the Clipboard Clipboard.SetImage(rtb); return true; } catch (Exception ex) { // Set the ErrorMessage property for caller to check after false return _ErrorMessage = ex.ToString(); return false; } } }}