screenshot



  • hallo

    Wie mache ich ein screenshot des desktops ?
    Im jpg dateiformat.?

    Nerd



  • Screenshot vom Desktop, gespeichert in einer BMP-Datei (nach jpg musst du selber gucken):

    void ScreenShotDesktop() {
      int     nWidth  = GetSystemMetrics(SM_CXSCREEN);
      int     nHeight = GetSystemMetrics(SM_CYSCREEN);
      HWND    hWnd    = ::GetDesktopWindow();
      HDC     hdc     = ::GetDC(hWnd);
      HDC     memDC   = ::CreateCompatibleDC(hdc);
      HBITMAP hbm     = ::CreateCompatibleBitmap(hdc, nWidth, nHeight);
      HBITMAP hbmOld  = (HBITMAP)::SelectObject(memDC, hbm);
      char sError[128];
    
      if(!::BitBlt(memDC,0,0,nWidth,nHeight,hdc,0,0,SRCCOPY)) {
    		//ERROR
    	}
    
      BITMAPINFO bmi;
      ZeroMemory(&bmi, sizeof(bmi));
      bmi.bmiHeader.biSize         = sizeof(BITMAPINFOHEADER);
      bmi.bmiHeader.biWidth        = nWidth;
      bmi.bmiHeader.biHeight       = nHeight;
      bmi.bmiHeader.biBitCount     = 24;
      bmi.bmiHeader.biPlanes       = 1;
      bmi.bmiHeader.biCompression  = BI_RGB;
      bmi.bmiHeader.biSizeImage    = 32 * nWidth * nHeight / 8;
    
    	BYTE *pbBits = new BYTE[bmi.bmiHeader.biSizeImage];
    
    	if(!::GetDIBits( memDC,hbm,0,bmi.bmiHeader.biHeight,pbBits,&bmi,DIB_RGB_COLORS)) {
    		//ERROR
    	}
    
    	BITMAPFILEHEADER bfh;
      bfh.bfType      = ('M' << 8) + 'B';
      bfh.bfSize      = sizeof(BITMAPFILEHEADER)  +
                        bmi.bmiHeader.biSizeImage +
                        sizeof(BITMAPINFOHEADER); 
      bfh.bfReserved1 = 0;
      bfh.bfReserved2 = 0;
      bfh.bfOffBits   = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    
    	if(!SetDIBits(memDC,hbm,0,bmi.bmiHeader.biHeight,pbBits,&bmi,DIB_RGB_COLORS)) {
        wsprintf(sError,"Error: SetDIBits (Code %i)\r\n",GetLastError());
        OutputDebugString(sError);
      }
    
    //  BitBlt(*hDC, 0, 0, GetSystemMetrics(SM_CXSCREEN)-1,GetSystemMetrics(SM_CYSCREEN)-1,memDC, 0, 0, SRCCOPY);
    
      HANDLE hfile = CreateFile( "__TMP__SCRNSHOT__2357__.BMP",
                                 GENERIC_WRITE,
                                 0,
                                 0,
                                 CREATE_ALWAYS,
                                 0,
                                 0 ); 
    
      if(hfile==INVALID_HANDLE_VALUE) {
        //ERROR
      }
      DWORD dwWritten;
      WriteFile(hfile,&bfh,           sizeof(bfh),               &dwWritten, NULL); 
      WriteFile(hfile,&bmi.bmiHeader, sizeof(BITMAPINFOHEADER),  &dwWritten, NULL); 
      WriteFile(hfile,pbBits,         bmi.bmiHeader.biSizeImage, &dwWritten, NULL); 
    
      CloseHandle(hfile); 
      ::SelectObject(memDC, hbmOld);
      ::DeleteDC(memDC);
      ::ReleaseDC(hWnd,hdc); 
      ::DeleteDC(hdc);
      ::DeleteObject(hbm);
      ::DeleteObject(hbmOld);
      ::DeleteObject(hWnd);
      hbm=NULL;
      hbmOld=NULL;
      delete[] pbBits;
    }
    


  • Hi

    in C hätte ich das gerne. !

    Nerd



  • nerd-- schrieb:

    Hi

    in C hätte ich das gerne. !

    Nerd

    Das ist C (mit WinAPI selbstverständlich). Was stört dich denn daran?



  • Ist es doch, bis auf ein new & delete 🙄



  • Note schrieb:

    Ist es doch, bis auf ein new & delete 🙄

    Ach stimmt, hab ich übersehen.

    Dann machst du da halt malloc/free draus und gut is'...



  • _matze schrieb:

    Note schrieb:

    Ist es doch, bis auf ein new & delete 🙄

    Ach stimmt, hab ich übersehen.

    Dann machst du da halt malloc/free draus und gut is'...

    + Scope-Operator



  • Wofür brauchst du die screenshots, im eigenen Programm zum weiteren Einsatz? Wenn du die nur ausserhalb des Programmes brauchst, z.B. für Dokumentationszwecke, empfiehlt sich besser der Einsatz eines geeigneten Bildbearbeitungsprogrammes. Zu nennen wäre Photoimpact, der macht das und kann selbst von BMP nach JPG umwandeln. Kann sehr viel eigene Arbeit ersparen.


  • Mod

    In Vista und Win7 ist das Snipping Tool ein geniales Bordwerkzeug.



  • Hi

    Nein möchte eine Highlevel funktion die mir ein Screenshot macht ! Das in C und der API, auch Dritt-Library's erwünscht !

    Programme die eine Konvertierung vornehmen, oder Bildbearbeitungs Programme, habe ich so viele das es mich erschlägt ! Nei, Scherz bei seite.

    Matze hat ein guten Ansatz gebracht. Doch leider ist das nicht reines C !

    Das ist jedenfals nicht C.

    ->    ::CreateCompatibleDC(hdc);
    

    Wohl eine Funktion der Api. Doch die funktionen ruft man da ohne " :: " auf.

    Und zum schluss.... Ich wollte so oder so eine Funktion die mir eine JPG Datei erstellt !

    lowbyte_



  • Hi

    Alles was ich Probiert habe hier ist mull ! syntaxfehler etc.

    Hier 2 Stunden müssame Arbeit.
    Ohne Syntaxfehler. Für C and C++;

    int main()
    {
    
      HDC hdc;
      HWND hWnd;
    
      hWnd = GetDesktopWindow();
      hdc = GetDC(hWnd); 
    
      dc2bitmap(hdc, 1680, 1050, "c:\\hallo.BMP");
    
      return 0;
    
    }
    
    void dc2bitmap(HDC hdc, int width, int height, char *filename)
    {
        HDC hdc2;
        HBITMAP aBmp;
        BITMAPINFO bi;
        HGDIOBJ OldObj;
        void *dibvalues;
        HANDLE fileHandle;
    
        BITMAPFILEHEADER bmfh;
        BITMAPINFOHEADER bmih;
            DWORD bytes_write;
        DWORD bytes_written;
    
        hdc2=CreateCompatibleDC(hdc);
    
        ZeroMemory(&bmih,sizeof(BITMAPINFOHEADER));
        bmih.biSize=sizeof(BITMAPINFOHEADER);
          bmih.biHeight=height;
            bmih.biWidth=width;
            bmih.biPlanes=1;
            bmih.biBitCount=24;
        bmih.biCompression=BI_RGB;
        bmih.biSizeImage = ((((bmih.biWidth * bmih.biBitCount) + 31) & ~31) >> 3) * bmih.biHeight;
            bmih.biXPelsPerMeter = 0;
            bmih.biYPelsPerMeter = 0;
            bmih.biClrImportant = 0;
        //bmih.biSizeImage=(3*bmih.biHeight*bmih.biWidth);
            //bmih.biSizeImage = 0;
    
        bi.bmiHeader=bmih;
    
        aBmp=CreateDIBSection(hdc,&bi,DIB_RGB_COLORS,(void**)&dibvalues,NULL,NULL);
    
        if (aBmp==NULL)
        {
            OutputDebugString("CreateDIBSection failed!\n");
            return 0;
        }
    
        OldObj=SelectObject(hdc2,aBmp);
        BitBlt(hdc2,0,0,width,height,hdc,0,0,SRCCOPY);
    
        ZeroMemory(&bmfh,sizeof(BITMAPFILEHEADER));
        bmfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
        bmfh.bfSize=(3*bmih.biHeight*bmih.biWidth)+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
            bmfh.bfType=0x4d42;
            bmfh.bfReserved1 = 0;
            bmfh.bfReserved2 = 0;
    
        fileHandle=CreateFile(filename,GENERIC_READ | GENERIC_WRITE,(DWORD)0,NULL,
                                                        CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL);
        if (fileHandle==INVALID_HANDLE_VALUE)
        {
            OutputDebugString("CreateFile failed!\n");
            return 0;
        }
    
            // Write the BITMAPFILEHEADER
            bytes_write=sizeof(BITMAPFILEHEADER);
        if (!WriteFile(fileHandle,(void*)&bmfh,bytes_write,&bytes_written,NULL))
        {
            OutputDebugString("WriteFile failed!\n");
            return 0;
        }
    
            //Write the BITMAPINFOHEADER
        bytes_write=sizeof(BITMAPINFOHEADER);
        if (!WriteFile(fileHandle,(void*)&bmih,bytes_write,&bytes_written,NULL))
        {
            OutputDebugString("WriteFile failed!\n");
            return 0;
        }
    
            //Write the Color Index Array???
            bytes_write=bmih.biSizeImage;//3*bmih.biHeight*bmih.biWidth;
        if (!WriteFile(fileHandle,(void*)dibvalues,bytes_write,&bytes_written,NULL))
        {
            OutputDebugString("WriteFile failed!\n");
            return 0;
        }
    
        CloseHandle(fileHandle);
    
        DeleteObject(SelectObject(hdc2,OldObj));
        DeleteDC(hdc2);
    
        return 1;
    
        }
    

    lowbyte


Anmelden zum Antworten