I would like to do function that user can capture specific screen area, and display them on other window. How can I do it with wpf?
The capture function look like "Rectangular Marquee Tool" of Adobe Photoshop but how can I get data of selected-screen.
I come with idea: 1. Make a transparent window 2. Capture selected screen behind it 3. Force selected-screen into image buffer 4. Open new window and display image buffer.
What are wpf controls should I use for this purpose?
-> What are wpf controls should I use for this purpose?
System.Windows.Controls.Image should be able to do the trick, you could call the GDI GetDesktopDC to do the trick, capture the screen into hBimtap, and bring the hBimtap into WPF world using Imaging.CreateBitmapSourceFromHBitmap() method.
class NativeMethods { // This is a call to unmanaged code. [DllImport("user32.dll")] //Get a pointer to the desktop window public extern static IntPtr GetDesktopWindow();
[DllImport("user32.dll")] //Get a device context from the desktop window public static extern IntPtr GetWindowDC(IntPtr hwnd); //[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")] [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleDC(IntPtr hdc); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight); [DllImport("gdi32.dll")] public static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp); [DllImport("gdi32.dll")] public static extern IntPtr DeleteObject(IntPtr hDc); [DllImport("gdi32.dll")] public static extern IntPtr DeleteDC(IntPtr hDc); [DllImport("user32.dll")] public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
[System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern bool BitBlt (IntPtr hDestDC, // handle to destination DC int nXDest, // X-coordinate of destination upper-left corner int nYDest, // Y-coordinate of destination upper-left corner int nWidthDest, // Width of destination rectangle int nHeightDest, // Height of destination rectangle IntPtr hSrcDC, // Handle to source DC int nXSrc, // X-coordinate of source upper-left corner int nYSrc, // Y-coordinate of source upper-left corner int rasterOp // raster operation code ); }
Have you tried directly passing the hBitmap returned from CreateCompatibleBitmap() to the Imaging.CreateBitmapSourceFromHBitmap() method without kicking off the System.Drawing.Bitmap intermediary at all?
I've done that before, and if I remember correctly, it can do the trick, let me know if it works for you.