/// <summary>
        /// Convert any control to a PngBitmapEncoder
        /// </summary>
        /// <param name="controlToConvert">The control to convert to an ImageSource</param>
        /// <returns>The returned ImageSource of the controlToConvert</returns>
        private static PngBitmapEncoder getImageFromControl(Control controlToConvert)
        {
            // save current canvas transform
            Transform transform = controlToConvert.LayoutTransform;

            // get size of control
            Size sizeOfControl = new Size(controlToConvert.ActualWidth, controlToConvert.ActualHeight);
            // measure and arrange the control
            controlToConvert.Measure(sizeOfControl);
            // arrange the surface
            controlToConvert.Arrange(new Rect(sizeOfControl));

            // craete and render surface and push bitmap to it
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap((Int32)sizeOfControl.Width, (Int32)sizeOfControl.Height, 96d, 96d, PixelFormats.Pbgra32);
            // now render surface to bitmap
            renderBitmap.Render(controlToConvert);
            
            // encode png data
            PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
            // puch rendered bitmap into it
            pngEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));

            // return encoder
            return pngEncoder;
        }
        public static void ScreenCapture(Control control, string filename)
        {
            try
            {
                // The BitmapSource that is rendered with a Visual.
                control.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                Size size = control.DesiredSize;
                int width = (int)size.Width;
                int height = (int)size.Height;
                control.Arrange(new Rect(0, 0, width, height));
                var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
                rtb.Render(control);

                // Encoding the RenderBitmapTarget as a PNG file.
                var png = new PngBitmapEncoder();
                png.Frames.Add(BitmapFrame.Create(rtb));
                using (Stream stm = File.Create(filename))
                {
                    png.Save(stm);
                }
            }
            finally
            {
            }
        }