Re: CreateDIBSection question

Tech-Archive recommends: Fix windows errors by optimizing your registry



On Thu, 31 Mar 2005 07:31:06 -0800, "james"
<james@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

>
>
>"Severian" wrote:
>
>> On Wed, 30 Mar 2005 20:06:52 GMT, Severian
>> <severian@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>>
>> >On Wed, 30 Mar 2005 10:05:05 -0800, "james"
>> ><james@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>> >
>> >>I've tried to write a code about CreateDIBSection but it don't work. I
>> >>would like to know why it don't work. This is the code about
>> >>DIBSection
>> >>
>> >> ULONG* theBits;
>> >> HWND hwnd_desktop = ::GetDesktopWindow();
>> >> HDC hdc_desktop = ::GetWindowDC(hwnd_desktop);
>> >>
>> >> CDC dc;
>> >> dc.Attach(hdc_desktop);
>> >>
>> >> CDC memDC;
>> >> memDC.CreateCompatibleDC(&dc);
>> >>
>> >> BITMAPINFOHEADER bitmapInfo;
>> >>
>> >> bitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
>> >> bitmapInfo.biWidth = horz;
>> >> bitmapInfo.biHeight = vert;
>> >> bitmapInfo.biPlanes = 1;
>> >> bitmapInfo.biBitCount = 32;
>> >> bitmapInfo.biCompression = BI_RGB;
>> >> bitmapInfo.biSizeImage = 0;
>> >> bitmapInfo.biXPelsPerMeter = 0;
>> >> bitmapInfo.biYPelsPerMeter = 0;
>> >> bitmapInfo.biClrUsed = 0;
>> >> bitmapInfo.biClrImportant = 0;
>> >>
>> >> HBITMAP bitmap =
>> >> ::CreateDIBSection(hdc_desktop,
>> >> (LPBITMAPINFO)&bitmapInfo,
>> >> DIB_RGB_COLORS,
>> >> (LPVOID *)&theBits,
>> >> NULL,
>> >> 0);
>> >
>> >From MSDN:
>> >
>> >` Return Values
>> >` If the function succeeds, the return value is a handle to the newly
>> >` created DIB, and *ppvBits points to the bitmap bit values.
>> >`
>> >` If the function fails, the return value is NULL, and *ppvBits is
>> >` NULL.
>> >`
>> >` Windows NT/2000/XP: To get extended error information, call
>> >` GetLastError. This can be the following value.
>> >`
>> >` Value Meaning
>> >` ERROR_INVALID_PARAMETER One or more input parameters is invalid.
>> >`
>> >
>> >1) What are horz & vert?
>> >
>> >2) Do you have enough memory for the bitmap (horz * vert * 4 bytes)?
>> >
>> >3) What O/S are you on?
>> >
>> >> memDC.SelectObject(&bitmap);
>> >>
>> >> memDC.BitBlt(0, 0, horz, vert, &dc, 0, 0, SRCCOPY);
>> >>
>> >>
>> >> for(x=0;x<end;x++)
>> >> Bitmap_1[x] = *theBits ++;
>> >>
>> >>With this code all *theBits calls return 0. Why it happen? Where is
>> >>the mistake?
>> >>I need to fill tha array Bitmap_1[x] with the desktop image using
>> >>DIBSection.
>> >
>> >If Bitmap_1[] is an array of ULONGs, it will be faster to do:
>> >
>> > memset(Bitmap_1, theBits, bitmapInfo.biSizeImage);
>>
>> memcpy, of course
>>
>> >I believe CreateDIBSection computes biSizeImage; if not, it's easy
>> >enough in your case since your pixel size is 4 bytes: (horz*vert*4).
>> >In other cases, you must compute a scan width (4-byte boundary) and
>> >multiply by the height.
>>
>> --
>> Sev
>>
>
>
>I have any error message or returned by GetLastError and i think the problem
>is between
>
>HBITMAP bitmap =
>::CreateDIBSection(hdc_desktop,
>(LPBITMAPINFO)&bitmapInfo,
>DIB_RGB_COLORS,
>(LPVOID *)&theBits,
>NULL,
>0);
>
>and
>
>memDC.BitBlt(0, 0, horz, vert, &dc, 0, 0, SRCCOPY);
>I have enough memory for it. Horz and vert are horizzontal and vertical
>dimension of the screen. My os is Win 98.
>
>"bitmap" and "theBits" are different than zero. I think the error is in
>BitBlt function. But where.
>
>Do you know a code about capturing desktop using DIBSection?

Not a lot. But if the system has multiple monitors, you're only going
to get the primary display.

Try:

HDC hdc_desktop = CreateDC("DISPLAY", NULL, NULL, NULL);


Be sure to DeleteDC(hdc_desktop), since it's not a window DC.

HTH

--
Sev
.



Relevant Pages

  • Re: a synchronization issue - jusy for you guys
    ... CreateEvent: ... If the function succeeds, the return value is a handle to the event ... the function returns a handle to the existing object and GetLastError ... To get extended error ...
    (comp.lang.pascal.delphi.misc)
  • Re: SetMenu & GetLastError
    ... > Can you tell me perhaps if it is for sure, that the value of GetLastError ... > statement in the MSDN, so I am a bit sceptical about this. ... If the function succeeds, ... as it doesn't prevent SetMenu from succeeding. ...
    (microsoft.public.win32.programmer.ui)