• gdi和direct截屏


    gdi截屏
    // TTTest.cpp : 定义应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "TTTest.h"
    #include <stdio.h>
    
    LPVOID    screenCaptureData=NULL;
    HBITMAP hBitmap;
    HDC hDDC;
    HDC hCDC;
    int nWidth;
    int nHeight;
    void initGDI(){
        // Get screen size
        nWidth = GetSystemMetrics(SM_CXSCREEN);
        nHeight = GetSystemMetrics(SM_CYSCREEN);
        
        screenCaptureData = new char[nWidth*nHeight*4];
        memset(screenCaptureData,0,nWidth);
    
        // Get desktop DC, create a compatible dc, create a comaptible bitmap and select into compatible dc.
        hDDC = GetDC( GetDesktopWindow() );
        hCDC = CreateCompatibleDC(hDDC);
        hBitmap =CreateCompatibleBitmap(hDDC,nWidth,nHeight);
        SelectObject(hCDC,hBitmap); 
    }
    void gdiScreenCapture(LPVOID screenCaptureData){
              
              
        BitBlt(hCDC,0,0,nWidth,nHeight,hDDC,0,0,SRCCOPY);
    
        GetBitmapBits(hBitmap,nWidth*nHeight*4,screenCaptureData);
        
    }
    
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
    initGDI();
    gdiScreenCapture(screenCaptureData);
    
    for(int i=0 ;i<nWidth*nHeight;i++){
        unsigned char b  = *((char*)screenCaptureData+i*4) ;
        unsigned char g  = *((char*)screenCaptureData+i*4+1) ;
        unsigned char r  = *((char*)screenCaptureData+i*4+2) ;
        unsigned char a  = *((char*)screenCaptureData+i*4+3) ;
    
        char chInput[10] ;
        sprintf(chInput,"x:%d y:%d A:%d R:%d G:%d B:%d\n",i%nWidth,i/nWidth,a,r,g,b);
        OutputDebugString( chInput );
    }
        return 0;
    }

     direct3d截屏

    // DirectScreenCapture.cpp : 定义应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "DirectScreenCapture.h"
    #include <d3d9.h>
    #include <WinError.h>
    
    IDirect3D9*            g_pD3DScreenCapture=NULL;
    IDirect3DDevice9*    g_pd3dDevice=NULL;
    IDirect3DSurface9*    g_pSurface=NULL;
    
    LPVOID    screenCaptureData=NULL;
    
    RECT        gScreenRect ={0,0,0,0};
    HRESULT    InitD3D()
    {
    
        
        BITMAPINFO    bmpInfo;
        ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
        bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
        bmpInfo.bmiHeader.biBitCount=32;
        bmpInfo.bmiHeader.biCompression = BI_RGB;
        bmpInfo.bmiHeader.biWidth=GetSystemMetrics(SM_CXSCREEN);
        bmpInfo.bmiHeader.biHeight=GetSystemMetrics(SM_CYSCREEN);
        bmpInfo.bmiHeader.biPlanes=1;
        bmpInfo.bmiHeader.biSizeImage=abs(bmpInfo.bmiHeader.biHeight)*bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biBitCount/8;
    
        HDC    hdc=GetDC(GetDesktopWindow());
        HDC        hBackDC=NULL;
        HBITMAP    hBackBitmap=NULL;
        hBackDC=CreateCompatibleDC(hdc);
        hBackBitmap=CreateDIBSection(hdc,&bmpInfo,DIB_RGB_COLORS,&screenCaptureData,NULL,0);
        if(hBackBitmap==NULL)
        {
            return 0 ;
        }
        ReleaseDC(GetDesktopWindow(),hdc);
    
    
    
        HWND hWnd = 0;
        D3DDISPLAYMODE    ddm;
        D3DPRESENT_PARAMETERS    d3dpp;
    
        if((g_pD3DScreenCapture=Direct3DCreate9(D3D_SDK_VERSION))==NULL)
        {
            return E_FAIL;
        }
    
        if(FAILED(g_pD3DScreenCapture->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&ddm)))
        {
            return E_FAIL;
        }
    
        ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
    
        d3dpp.Windowed=true;
        d3dpp.Flags=D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
        d3dpp.BackBufferFormat=ddm.Format;
        d3dpp.BackBufferHeight=gScreenRect.bottom =ddm.Height;
        d3dpp.BackBufferWidth=gScreenRect.right =ddm.Width;
        d3dpp.MultiSampleType=D3DMULTISAMPLE_NONE;
        d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
        d3dpp.hDeviceWindow=hWnd;
        d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_DEFAULT;
        d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT;
    
        if(FAILED(g_pD3DScreenCapture->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING ,&d3dpp,&g_pd3dDevice)))
        {
            return E_FAIL;
        }
    
        if(FAILED(g_pd3dDevice->CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &g_pSurface, NULL)))
        {
            return E_FAIL;
        }
    
        return S_OK;
    }
    //direct实现的截图
    void directScreenCapture(LPVOID screenCaptureData){
        g_pd3dDevice->GetFrontBufferData(0, g_pSurface);
        D3DLOCKED_RECT    lockedRect;
        if(FAILED(g_pSurface->LockRect(&lockedRect,&gScreenRect,D3DLOCK_NO_DIRTY_UPDATE|D3DLOCK_NOSYSLOCK|D3DLOCK_READONLY)))
        {
            return;
        }
        for(int i=0;i<gScreenRect.bottom;i++)
        {
            memcpy((BYTE*)screenCaptureData+(i)*gScreenRect.right*32/8,(BYTE*)lockedRect.pBits+i*lockedRect.Pitch,gScreenRect.right*32/8);
        }
        g_pSurface->UnlockRect();
        //printf("%d\n" , l3);
    }
    
    
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
        //初始化direct3d,用户截屏
        if(FAILED(InitD3D()))    return -1;
        directScreenCapture(screenCaptureData);
        unsigned char * screenData = (unsigned char *)screenCaptureData ;
        for(int y=0 ; y<1080 ;y++){
            for(int x =0 ; x<1920 ;x ++){
                int offset = (x + y * 1080) *4;
                unsigned char b = *(screenData + offset);
                unsigned char g = *(screenData + offset + 1);
                unsigned char r = *(screenData + offset + 2);
                unsigned char a = *(screenData + offset + 3);
            }
        }
        return 0;
    }

    项目图:

    direct截屏、c++DLL封装、c#显示,图片项目

  • 相关阅读:
    lvs+keeplived笔录
    python之购物车的编写(熬夜撸代码中。。。)
    关于三级菜单程序的编写
    .split()函数使用方法
    range()函数的使用
    关于python如何简单跳出多层循环
    Kubernetes部署通用手册 (支持版本1.19,1.18,1.17,1.16)
    八个开源的 Spring Boot 前后端分离项目,一定要收藏!
    招聘简章-2020年10月25日19:31:39
    小公司老板的日常管理
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/3012556.html
Copyright © 2020-2023  润新知