public ImageSource GetWindowImage(IntPtr hWnd, FormsDraw.Rectangle bounds) { using (FormsDraw.Bitmap result = new FormsDraw.Bitmap(bounds.Width, bounds.Height)) { //creates a new Bitmap which will contain the window's image using (FormsDraw.Graphics MemG = FormsDraw.Graphics.FromImage(result)) { //makes a Graphics from the Bitmap whose HDC will hold the window's RAW image IntPtr dc = MemG.GetHdc(); //gets the Bitmap's HDC try { WinUtils.PrintWindow(hWnd, dc, 0);//Writes the window's image to the HDC created above } finally { MemG.ReleaseHdc(dc); } return(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(result.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); } } }
public void MakeWindow(IntPtr hWnd) { Image window = new Image(); window.Width = 240; window.Height = 240; window.Stretch = Stretch.Fill; WinUtils.Rect rect = new WinUtils.Rect(); //makes a new Rect which will hold the target window size WinUtils.GetWindowRect(hWnd, ref rect); //gets the target window size FormsDraw.Rectangle bounds = new FormsDraw.Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top); //converts the rect into something usable FormsDraw.Bitmap result = new FormsDraw.Bitmap(bounds.Width, bounds.Height); //creates a new Bitmap which will contain the window's image FormsDraw.Graphics MemG = FormsDraw.Graphics.FromImage(result); //makes a Graphics from the Bitmap whose HDC will hold the window's RAW image IntPtr dc = MemG.GetHdc(); //gets the Bitmap's HDC WinUtils.PrintWindow(hWnd, dc, 0); //Writes the window's image to the HDC created above MemG.ReleaseHdc(dc); window.Source = WinUtils.ImageSourceFromBitmap(result); WinMgr.Children.Add(window);//adds the PB to the FlowLayoutPanel }