Remove alpha from BitmapSource

I use BitBlt () and CreateBitmapSourceFromHBitmap () to capture a window as a BitmapSource, which I can display in the Image element in a WPF application. But for some reason, most of the recording application is transparent. Here is a source against capturing an image of what is happening:

https://userpages.umbc.edu/~smyth1/images/screencap.PNG
(source: umbc.edu )

It is gray because the background of the window on which it is located is gray. Whatever background I give the window, it shines through.

How can I get the captured image to more accurately reflect the original?

+1
source share
1 answer

API Win32 (CreateCompatibleDC, SelectObject, CreateBitmap...). , GetDC BitBlt, . :

    public static Bitmap Capture(IntPtr hwnd)
    {
        IntPtr hDC = GetDC(hwnd);
        if (hDC != IntPtr.Zero)
        {
            Rectangle rect = GetWindowRectangle(hwnd);
            Bitmap bmp = new Bitmap(rect.Width, rect.Height);
            using (Graphics destGraphics = Graphics.FromImage(bmp))
            {
                BitBlt(
                    destGraphics.GetHdc(),
                    0,
                    0,
                    rect.Width,
                    rect.Height,
                    hDC,
                    0,
                    0,
                    TernaryRasterOperations.SRCCOPY);
            }
            return bmp;
        }
        return null;
    }

Windows Forms WPF ( Imaging.CreateBitmapSourceFromHBitmap), ( SO Firefox).

,

+4

Source: https://habr.com/ru/post/1723237/


All Articles