Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7571059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:34:43+00:00 2026-05-30T15:34:43+00:00

Possible Duplicate: how to make screen screenshot with win32 in c++? I am currently

  • 0

Possible Duplicate:
how to make screen screenshot with win32 in c++?

I am currently trying to create an application that saved a portion of the screen to a bmp. I have found BitBlt but I really don’t know what to do with it. I have tried searching for some answers but I still haven’t found a clarifying one using C++.

So, basically I want this function:

bool capturePartScreen(int x, int y, int w int, h, string dest){
    //Capture part of screen according to coordinates, width and height.
    //Save that captured image as a bmp to dest.
    //Return true if success, false if failure
}

BitBlt:

BOOL BitBlt(
    __in  HDC hdcDest,
    __in  int nXDest,
    __in  int nYDest,
    //The three above are the ones I don't understand!
    __in  int nWidth,
    __in  int nHeight,
    __in  HDC hdcSrc,
    __in  int nXSrc,
    __in  int nYSrc,
    __in  DWORD dwRop
);

What should that hdc be and how do I obtain the bmp?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:34:45+00:00Added an answer on May 30, 2026 at 3:34 pm

    It took a while, but I have now finally ended up with a functioning script.

    Requirements:

    #include <iostream>
    #include <ole2.h>
    #include <olectl.h>
    

    Also you might(?) have to add ole32, oleaut32 and uuid to your linker.

    screenCapturePart:

    bool screenCapturePart(int x, int y, int w, int h, LPCSTR fname){
        HDC hdcSource = GetDC(NULL);
        HDC hdcMemory = CreateCompatibleDC(hdcSource);
    
        int capX = GetDeviceCaps(hdcSource, HORZRES);
        int capY = GetDeviceCaps(hdcSource, VERTRES);
    
        HBITMAP hBitmap = CreateCompatibleBitmap(hdcSource, w, h);
        HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMemory, hBitmap);
    
        BitBlt(hdcMemory, 0, 0, w, h, hdcSource, x, y, SRCCOPY);
        hBitmap = (HBITMAP)SelectObject(hdcMemory, hBitmapOld);
    
        DeleteDC(hdcSource);
        DeleteDC(hdcMemory);
    
        HPALETTE hpal = NULL;
        if(saveBitmap(fname, hBitmap, hpal)) return true;
        return false;
    }
    

    saveBitmap:

    bool saveBitmap(LPCSTR filename, HBITMAP bmp, HPALETTE pal)
    {
        bool result = false;
        PICTDESC pd;
    
        pd.cbSizeofstruct   = sizeof(PICTDESC);
        pd.picType      = PICTYPE_BITMAP;
        pd.bmp.hbitmap  = bmp;
        pd.bmp.hpal     = pal;
    
        LPPICTURE picture;
        HRESULT res = OleCreatePictureIndirect(&pd, IID_IPicture, false,
                           reinterpret_cast<void**>(&picture));
    
        if (!SUCCEEDED(res))
        return false;
    
        LPSTREAM stream;
        res = CreateStreamOnHGlobal(0, true, &stream);
    
        if (!SUCCEEDED(res))
        {
        picture->Release();
        return false;
        }
    
        LONG bytes_streamed;
        res = picture->SaveAsFile(stream, true, &bytes_streamed);
    
        HANDLE file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, 0,
                     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    
        if (!SUCCEEDED(res) || !file)
        {
        stream->Release();
        picture->Release();
        return false;
        }
    
        HGLOBAL mem = 0;
        GetHGlobalFromStream(stream, &mem);
        LPVOID data = GlobalLock(mem);
    
        DWORD bytes_written;
    
        result   = !!WriteFile(file, data, bytes_streamed, &bytes_written, 0);
        result  &= (bytes_written == static_cast<DWORD>(bytes_streamed));
    
        GlobalUnlock(mem);
        CloseHandle(file);
    
        stream->Release();
        picture->Release();
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP make if shorter I have an if statement that looks like
Possible Duplicate: how to make phone call by using objective c? I am trying
Possible Duplicate: How to make return key on iphone make keyboard disappear? I have
Possible Duplicate: How to make the startup form initially invisible or hidden My application
Possible Duplicate: how to make surfaceview transparent I am trying to make a custom
Possible Duplicate: make drop down list item unselectable I have an asp.net dropdownlist and
Possible Duplicate: C# - Make a borderless form movable? I have made a form
Possible Duplicate: Method for full-screen vertically-centered HTML page? Pls tell me how to make
Possible Duplicate: how to make “pretty rounding” in R? I have a number, say
Possible Duplicate: How to make in Javascript full screen windows (stretching all over the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.