Solved

How to capture a DIB to a 24 bpp

Posted on 2001-08-30
4
411 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 200 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now