• Advertisement

Archived

This topic is now archived and is closed to further replies.

HDC --> HBITMAP

This topic is 5687 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

IS IT POSSIBLE ??!!! Okay, I have been screwing with this for... 9 hours now. I am SICK and TIRED of HBITMAP this and HDC that. This is for C++ Programming using the Windows API (XP,98,whatever). Please, someone help me... I do not want to embarass myself further by saying WHY I need this so desperately (I do not really need it at all), but now that I am into it, I just want an answer. I need to copy whatever crap is on the HDC to an HBITMAP. It cannot be THAT hard, can it. I can easily go the other way (HBITMAP-->HDC), but this is really starting to annoy me... I need sleep. So, if anyone can help me out, I would greatly appreciate it... (Oh, and please do not tell me to refer to any help files or simply say GetDIBits()... I need an example that does not totally suck... THANK YOU SOOOO MUCH in advance.)

Share this post


Link to post
Share on other sites
Advertisement
SON OF A BITCH !!

Okay, I finally managed to copy some bits around (NOT HDC --> HBITAMP) from HBITMAP-->HBITMAP. I need someone near me to know how to program in Windows...

(Keep the help coming.)

Share this post


Link to post
Share on other sites
Here''s some untested stuff I posted earlier on these forums:

  
// Use helpful macros from windowsx.h

#include <windowsx.h>
// Need this temporary bitmap so that we can select it

HBITMAP hbmTemp = CreateCompatibleBitmap(hdc, 1, 1);
// SelectBitmap will return current bitmap in DC, which has the DC contents

HBITMAP hbmCurrent = SelectBitmap(hdc, hbmTemp);
//

// You can get access to the DC contents in device-specific format by calling GetObject

// This structure holds information about bitmap

BITMAP Info;
// Retrieve bitmap information

GetObject(hbmCurrent, &Info, sizeof Info);
// lpBitmapData will point to the bitmap data in DC-specific format

LPVOID lpBitmapData = Info.bmBits;
//

// Alternatively, you can store the data in a device-independent bitmap,

// whose internal format is known (array of RGBQUADs)

// First, we need to fill BITMAPINFO struct

BITMAPINFO DibInfo = {
{ sizeof (BITMAPINFOHEADER), Info.bmWidth, Info.bmHeight, 1, 32 }
};
// This variable will point to the DIB image data

LPVOID lpDibData;
// Create device independent bitmap

HBITMAP hbmDIB = CreateDIBSection(hdc, &DibInfo, DIB_RGB_COLORS, &lpDibData, 0, 0);
// Copy device image into our DIB

GetDIBits(hdc, hbmCurrent, 0, Info.bmHeight, lpDibData, &DibInfo, DIB_RGB_COLORS);
// You can now use lpDibData to store contents of DC in a file, etc.

// When you''re done, delete the DIB section

DeleteBitmap(hbmDIB);
//

// When you''re done with the DC image, select it back to the DC

SelectBitmap(hdc, hbmCurrent);
// Delete temporary bitmap now

DeleteBitmap(hbmTemp);

Share this post


Link to post
Share on other sites
To transfer data from DC to Bitmap and vice versa you need to blit between two compatible DCs that have bitmaps selected. The following code fragment shows how to programmatically take a screenshot from within a Win32 application:


  
// access desktop DC

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

// create a compatible DC to hold our bitmap

HDC hCompDC = CreateCompatibleDC(hScreen);

// get width and height of desktop

int nWidth = GetSystemMetrics(SM_CXSCREEN);
int nHeight = GetSystemMetrics(SM_CYSCREEN);

// create bitmap to receive the screenshot

HBITMAP hBmp = CreateCompatibleBitmap(hScreen, nWidth, nHeight);

// select bitmap into target DC

HBITMAP hOld = (HBITMAP)SelectObject(hCompDC, hBmp);

// blit bits from screen to target buffer

BitBlt(hCompDC, 0, 0, nWidth, nHeight, hScreen, 0, 0, SRCCOPY);

// de-select bitmap

SelectObject(hCompDC, hOld);

// free DCs

DeleteDC(hScreen);
DeleteDC(hCompDC);

// save image or whatever

CBmpImage img;
img.SetBitmap(hBmp);
img.Save("C:\\WINME\\Desktop\\Screenshot.bmp");


Share this post


Link to post
Share on other sites
Thanks a lot, guys. I just got a lot of sleep, and then I went out and pole-fought with 9-ft long cardboard poles. I feel SO much better, now.

I suppose that my last question is: What is MicroSoft''s format for its cute, little "array of bytes" that are the BitmapBits?

I just want to darken the bitmap by 0x99999999, and before I simultaneously try random things and check the ''net, I wanted to know if anyone here knew.

Good luck !!

Share this post


Link to post
Share on other sites

  • Advertisement