?
Solved

How to capture a DIB to a 24 bpp

Posted on 2001-08-30
4
Medium Priority
?
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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