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

  • Home
  • SEARCH
  • 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 3664652
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:40:16+00:00 2026-05-19T01:40:16+00:00

I’ve been searching for some alternative solutions to capturing the screen to a bitmap

  • 0

I’ve been searching for some alternative solutions to capturing the screen to a bitmap on a windows machine.

Now, I am aware that PIL has an ImageGrab library and I will probably end up using it.
However, during my searches I came across a solution that uses ctypes to access gdi32.dll functions and use those to capture the screen.
There was one particular solution I came across which doesn’t work, and I’ve been trying to figure out what’s wrong it.

Here’s the source code:

from ctypes import *

class Bitmap(Structure):
    _fields_ = [("bitmapType", c_long),
                ("width", c_long),
                ("height", c_long),
                ("widthBytes", c_long),
                ("planes", c_short),
                ("bitsPerPixel", c_short),
                ("data", POINTER(c_ulong))]

if __name__ == "__main__":
    user32 = WinDLL("user32.dll")
    gdi32 = WinDLL("gdi32.dll")

    #Constants
    SM_CXSCREEN = 0
    SM_CYSCREEN = 1
    SRCCOPY = 0xCC0020

    #Capture the Bitmap
    width = user32.GetSystemMetrics(SM_CXSCREEN)
    height = user32.GetSystemMetrics(SM_CYSCREEN)
    screenDC = user32.GetWindowDC(user32.GetDesktopWindow())
    captureDC = gdi32.CreateCompatibleDC(screenDC)
    captureBitmap = gdi32.CreateCompatibleBitmap(screenDC, width, height)
    gdi32.SelectObject(captureDC, captureBitmap)
    gdi32.BitBlt(captureDC, 0, 0, width, height, screenDC, 0, 0, SRCCOPY)

    picture = Bitmap()
    gdi32.GetObjectA(captureBitmap, 24, byref(picture))

Now, to me it seems that in the end of the program, the author tries to copy the bitmap to the Bitmap object, but:
1. I have failed to find documentation of the gdi32.GetObjectA function
2. When trying to view the data inside picutre.data (I hope I was not wrong by accessing picture.data.contents), I get a Value error: “NULL pointer access”.

Now, in the page where I found this code snippet, there was an answer to the problem, but it was a bit vague and not very informative. The answer read:

You must allocate the integer array for the data pointer of the BITMAP structure.
GetObject doesn't it for you.

Shouldn’t the gdi32.CreateCompatibleBitmap function already allocate that memory, and doesn’t the GetObject just copy the pointer to the Python representation of the bitmap structure?

I’m really confused here, any light shed on this problem would be really appreciated.
(I already have like 15 open tabs with MSDN gdi32 references and 15 more with ctypes references, but seeing as I don’t have a solid grasp on either, I feel like I’m going around in circles)

  • 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-19T01:40:16+00:00Added an answer on May 19, 2026 at 1:40 am

    GetObject does not return the whole bitmap. picture.data is actually NULL after the GetObject call. You’ll need GetDIBits to read the bitmap. Here’s the relevant excerpt from the GetObject docs.

    If hgdiobj is a handle to a bitmap
    created by calling CreateDIBSection,
    and the specified buffer is large
    enough, the GetObject function returns
    a DIBSECTION structure. In addition,
    the bmBits member of the BITMAP
    structure contained within the
    DIBSECTION will contain a pointer to
    the bitmap’s bit values.

    If hgdiobj is a handle to a bitmap
    created by any other means, GetObject
    returns only the width, height, and
    color format information of the
    bitmap
    . You can obtain the bitmap’s
    bit values by calling the GetDIBits or
    GetBitmapBits function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.