简体   繁体   中英

how to capture screen picture with normal aspect ratio in c#

i have wide screen monitor and when i capture screen picture via win API in c# the captured picture will be wide. but i want to take normal aspect ratio 1:1 picture. i don't want take picture first, then resize it because items on image will be malformed.

how i can do that? is this possible?

i am using this code :

    public static Bitmap GetDesktopImage()
    {
        //In size variable we shall keep the size of the screen.

        SIZE size;


        IntPtr  hDC = PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow()); 


        IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);


        do{
        size.cx = PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CXSCREEN);

        size.cy = PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CYSCREEN);



            m_HBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hDC, size.cx, size.cy);

        } while (m_HBitmap == IntPtr.Zero);
        if (m_HBitmap!=IntPtr.Zero)
        {
            IntPtr hOld = (IntPtr) PlatformInvokeGDI32.SelectObject(hMemDC, m_HBitmap);

            PlatformInvokeGDI32.BitBlt(hMemDC,0, 0,size.cx,size.cy, hDC, 0, 0, PlatformInvokeGDI32.SRCCOPY);
            PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
            PlatformInvokeGDI32.DeleteDC(hMemDC);
            PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(), hDC);
            Bitmap res=System.Drawing.Image.FromHbitmap(m_HBitmap);
            PlatformInvokeGDI32.DeleteObject(m_HBitmap);
            return res;

        }

        return null;
    }

you would need to crop part of the screen. you can't convert 16:9 to 4:3 without cropping.

-OR-

Change the screens resolution to 4:3 resolution, Take the screen shot, Change it back.

Call GetWindowRect on the desktop window handle to get the size. I don't think the GetSystemMetric call you use takes the menu bar and such into account.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM