Solved

How to capture a DIB to a 24 bpp

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 114
Dynamically allocate memory 9 57
Better understanding on C++ Class serialization and formats 9 61
What is sub-make ? 2 40
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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

863 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

27 Experts available now in Live!

Get 1:1 Help Now