?
Solved

How to capture a DIB to a 24 bpp

Posted on 2001-08-30
4
Medium Priority
?
452 Views
Last Modified: 2008-02-01
I would like to capture a bitmap from my screen and store it in a format which is independent of the resolution. More Specifically, I would like to store it always in a 24 bpp file. Is there any way to do this.
   However I cannot use GetDIBits(), because of performance issues.
    This is because i get the buffer directly from surface memory(using directx) and i will not be using windows gdi functions because of there slower performance.




0
Comment
Question by:sinha_sandeep
4 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 6443755
>>However I cannot use GetDIBits(), because of performance issues.

There is no way to get the image data to a file without using GetDIBits().  Anyway, the performce issues will be all about the transfer of the data to disk -- not any copying of bits from memroy buffer to memroty buffer.

I can tell you how to take an image from the screen and blit it onto a 24bpp memory DC and from there move it to a file.  But this other issue (refusal to use GetDIBits) needs to be addressed first.

-- Dan
0
 
LVL 1

Accepted Solution

by:
AlexReser earned 800 total points
ID: 6444069
This code shows how to drop whole screen in 24-bpp BMP-file:

int iScrWidth = GetSystemMetrics(SM_CXSCREEN);
int iScrHeight = GetSystemMetrics(SM_CYSCREEN);

HDC hdcScreen = GetDC(NULL);

BITMAPINFO bmpInfo;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth              = iScrWidth;
bmpInfo.bmiHeader.biHeight             = iScrHeight;
bmpInfo.bmiHeader.biPlanes             = 1;
bmpInfo.bmiHeader.biBitCount           = 24;
bmpInfo.bmiHeader.biCompression        = BI_RGB;
bmpInfo.bmiHeader.biSizeImage          = 0;
bmpInfo.bmiHeader.biXPelsPerMeter      = 0;
bmpInfo.bmiHeader.biYPelsPerMeter      = 0;
bmpInfo.bmiHeader.biClrUsed            = 0;
bmpInfo.bmiHeader.biClrImportant       = 0;

void *pvBits = NULL;
HBITMAP hBitmap = CreateDIBSection(hdcScreen,&bmpInfo,DIB_RGB_COLORS,&pvBits,NULL,0);
HDC hdcCompatible = CreateCompatibleDC(hdcScreen);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcCompatible,hBitmap);
BOOL b = BitBlt(hdcCompatible,0,0,iScrWidth,iScrHeight,hdcScreen,0,0,SRCCOPY);

HANDLE hFile = CreateFile("yourbmp.bmp",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,
           CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
   DWORD dwCnt;
   char z[14];
   ZeroMemory(z,14);
   strcpy(z,"BM8");
   z[4] = 0x24;
   z[10] = 0x36;
   WriteFile(hFile,z,14,&dwCnt,NULL);
   WriteFile(hFile,(char*)&bmpInfo,sizeof(BITMAPINFO)-4,&dwCnt,NULL);
   WriteFile(hFile,(char*)pvBits,iScrWidth*iScrHeight*3,&dwCnt,NULL);
   CloseHandle(hFile);
}    
   
SelectObject(hdcCompatible,hOldBitmap);
DeleteDC(hdcCompatible);
DeleteObject(hBitmap);
ReleaseDC(NULL,hdcScreen);
0
 
LVL 11

Expert Comment

by:griessh
ID: 6865651
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "AlexReser" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6886455
Per recommendation, force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question