3

I capture images using BitBlt / StretchBlt API's on windows 10, depending upon the necessity for scaling the screen or not. Whenever I capture the 2nd monitor that is not being configured as primary, there seems to be a strange behaviour that the captured screen's first half is the replica of the second half of the screen. This occurs randomly once for every 10 images on an average.

I could not find any documents over the internet related to this kind of strange problems. I'm not even doing any multi-threading. Might there be a problem with the capturing frequency?, But that doesn't even look sense, as there is no issue with screen capture whenever I set the monitor as primary.

Here is the original image

Original image

captured image

Captured image

monitor alignment

Monitor alignment

Note: I have been able to reproduce the issue only when the forground window is Notepad++ as of now. I'm not sure whether the issue happens for other applications as well., I could not go for directX based approach as the program must run on WindowsXP as well.

Here is an excerpt of the code:

HDC memDC = CreateCompatibleDC ( hDC ) ;

HBITMAP previousImage = ( HBITMAP ) SelectObject ( memDC , curImage ) 

if ( ( previousImage ) == NULL )
{
    return -1;
}

BOOL blitok ;

if ( lf_ImageScalingFactor != (double)1.0 )
{
    SetStretchBltMode ( memDC , HALFTONE ) ;

    if ( capture_all )
    {
        blitok = StretchBlt ( memDC , 0 , 0 , (int)(clientwindow.Width () ) , (int) (clientwindow.Height () ), 
            hDC , clientwindow.left - (physicalscreenrect.left * lf_ImageScalingFactor), clientwindow.top - (physicalscreenrect.top * lf_ImageScalingFactor), physicalscreenrect.Width(), physicalscreenrect.Height(), SRCCOPY | CAPTUREBLT ) ;
    }
    else
    {
        blitok = StretchBlt ( memDC , 0 , 0 , (int)(clientwindow.Width ()) , (int)(clientwindow.Height ()), 
                hDC , clientwindow.left - (physicalscreenrect.left * lf_ImageScalingFactor), clientwindow.top - (physicalscreenrect.top * lf_ImageScalingFactor),physicalscreenrect.Width(), physicalscreenrect.Height(), SRCCOPY ) ;
    }
}
else
{
    if ( capture_all )
    {
        blitok = BitBlt ( memDC , 0 , 0 , clientwindow.Width (), clientwindow.Height (), 
                hDC , clientwindow.left - physicalscreenrect.left , clientwindow.top - physicalscreenrect.top , SRCCOPY | CAPTUREBLT ) ;
    }
    else
    {
        blitok = BitBlt ( memDC , 0 , 0 , clientwindow.Width (), clientwindow.Height (), 
                hDC , clientwindow.left - physicalscreenrect.left , clientwindow.top - physicalscreenrect.top , SRCCOPY ) ;
    }

}

SelectObject ( memDC , previousImage ) ;

DeleteDC ( memDC ) ;
DeleteDC ( hDC ) ;

Am I doing something wrong over here?.

Any help is appreciable, Thanks in advance.

iamrameshkumar
  • 1,328
  • 1
  • 14
  • 34
  • 1
    DPI-awareness is the usual problem, especially so on Win10 since its installer no longer picks 96 dpi as the default. Well, you have it at 150% (aka 144 dpi) so if you don't include dpiAware in the manifest then you'll be lied to about the position and size of other windows. – Hans Passant Apr 19 '17 at 10:48
  • Thanks @Hans Passant your suggestion was really useful, I checked the dpi of the 2 monitors and they are different.The problem is gone when I set the same dpi for both of them. I did not understand what is happening though I had set the per monitor dpi awareness to true. – iamrameshkumar Apr 19 '17 at 13:15
  • Per-monitor DPI awareness is an issue in Windows versions >= 8.1. Requires a different value in the manifest. Googles well. – Hans Passant Apr 19 '17 at 13:31
  • You mean true/PM would not work properly on win 8.1 and greater? – iamrameshkumar Apr 19 '17 at 17:18
  • @Hans Passant just for the clarification, it looks like the DPI awareness works fine, I tried with and without DPI awareness for whole system as well as per-monitor DPI setting. Otherwise it couldn't have captured other windows perfectly without any problems, I see the issue occurs only when I move the mouse over the particular notepad++ window and that too in a random manner. As it is not in full screen and I could see the other windows being captured properly. – iamrameshkumar Apr 19 '17 at 18:12
  • Whenever I run my application with admin privileges with the same DPI & Monitor settings, that issue is gone. I don't understand what is happening. – iamrameshkumar Apr 19 '17 at 18:15

0 Answers0