Example #1
0
        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------

        #region Public methods.

        /// <summary>
        /// Creates a bitmap with the image a user sees for the specified
        /// element.
        /// </summary>
        /// <param name="element">Element to create bitmap for.</param>
        /// <returns>The created bitmap.</returns>
        public static Bitmap CreateBitmapFromElement(
            UIElement element)
        {
            new System.Security.Permissions.UIPermission(
                System.Security.Permissions.PermissionState.Unrestricted)
            .Assert();

            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            IntPtr    windowHandle = GetWindowHandleFromElement(element);
            Rectangle elementRect  = GetTopLevelClientRelativeRect(element);

            using (Bitmap bitmap = CreateBitmapFromWindowHandle(windowHandle))
            {
                // Scale elementRect to proper (bitmap's) DPI
                elementRect = BitmapUtils.AdjustBitmapSubAreaForDpi(bitmap, elementRect);
                return(BitmapUtils.CreateSubBitmap(bitmap, elementRect));
            }
        }