~edwin-grubbs/python-imaging/trunk

« back to all changes in this revision

Viewing changes to display.c

  • Committer: effbot
  • Date: 2006-03-01 19:11:48 UTC
  • Revision ID: svn-v4:be285980-f00d-0410-a9fe-d4747b46ecd0:pil:18
Load Imaging-1.1.3 into pil.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * The Python Imaging Library.
3
 
 * $Id$
 
3
 * $Id: //modules/pil/display.c#3 $
4
4
 *
5
5
 * display support
6
6
 *
7
7
 * History:
8
 
 * 96-05-13 fl  Windows DIB support
9
 
 * 96-05-21 fl  Added palette stuff
10
 
 * 96-05-28 fl  Added display_mode stuff
11
 
 * 97-09-21 fl  Added draw primitive
 
8
 * 1996-05-13 fl  Windows DIB support
 
9
 * 1996-05-21 fl  Added palette stuff
 
10
 * 1996-05-28 fl  Added display_mode stuff
 
11
 * 1997-09-21 fl  Added draw primitive
 
12
 * 2001-09-17 fl  Added ImagingGrabScreen (from _grabscreen.c)
12
13
 *
13
14
 * Copyright (c) Secret Labs AB 1997.
14
15
 * Copyright (c) Fredrik Lundh 1996-97.
202
203
    return Py_BuildValue("s(ii)", mode, size[0], size[1]);
203
204
}
204
205
 
 
206
PyObject*
 
207
PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)
 
208
{
 
209
    int width, height;
 
210
    HBITMAP bitmap;
 
211
    BITMAPCOREHEADER core;
 
212
    HDC screen, screen_copy;
 
213
    PyObject* buffer;
 
214
    
 
215
    /* step 1: create a memory DC large enough to hold the
 
216
       entire screen */
 
217
 
 
218
    screen = CreateDC("DISPLAY", NULL, NULL, NULL); 
 
219
    screen_copy = CreateCompatibleDC(screen); 
 
220
 
 
221
    width = GetDeviceCaps(screen, HORZRES);
 
222
    height = GetDeviceCaps(screen, VERTRES);
 
223
 
 
224
    bitmap = CreateCompatibleBitmap(screen, width, height);
 
225
    if (!bitmap)
 
226
        goto error;
 
227
        
 
228
    if (!SelectObject(screen_copy, bitmap))
 
229
        goto error;
 
230
 
 
231
    /* step 2: copy bits into memory DC bitmap */
 
232
 
 
233
    if (!BitBlt(screen_copy, 0, 0, width, height, screen, 0, 0, SRCCOPY))
 
234
        goto error;
 
235
 
 
236
    /* step 3: extract bits from bitmap */
 
237
 
 
238
    buffer = PyString_FromStringAndSize(NULL, height * ((width*3 + 3) & -4));
 
239
    if (!buffer)
 
240
        return NULL;
 
241
 
 
242
    core.bcSize = sizeof(core);
 
243
    core.bcWidth = width;
 
244
    core.bcHeight = height;
 
245
    core.bcPlanes = 1;
 
246
    core.bcBitCount = 24;
 
247
    if (!GetDIBits(screen_copy, bitmap, 0, height, PyString_AS_STRING(buffer),
 
248
                   (BITMAPINFO*) &core, DIB_RGB_COLORS))
 
249
        goto error;
 
250
 
 
251
    DeleteObject(bitmap);
 
252
    DeleteDC(screen_copy);
 
253
    DeleteDC(screen);
 
254
 
 
255
    return Py_BuildValue("(ii)N", width, height, buffer);
 
256
 
 
257
error:
 
258
    PyErr_SetString(PyExc_IOError, "screen grab failed");
 
259
 
 
260
    DeleteDC(screen_copy);
 
261
    DeleteDC(screen);
 
262
 
 
263
    return NULL;
 
264
}
 
265
 
205
266
#endif /* WIN32 */