Solved

32bpp->24bpp with BitBlt/StretchBlt

Posted on 2002-03-07
13
1,446 Views
Last Modified: 2007-12-19
In my program, it is necessary to convert the screen capture image from the screen's bit depth (usually
32bpp) to a 24bit format. With this code, I get about 0.5 to 1.0 seconds of lag (system freeze; the
mouse cannot be moved) during StretchBlt() when converting from my desktop depth of 32bits to 24bits.
As far as I can tell, this could perhaps be windows XP dependant?

Here's the code:

HBITMAP Bitmap;
int ScreenWidth, ScreenHeight, Width, Height, Scale;
HDC DesktopDC, CaptureDC;
BITMAPINFO BitmapInfo;
unsigned char *Data;

Scale = 50;
DesktopDC = GetDC(GetDesktopWindow());
ScreenWidth = GetDeviceCaps(DesktopDC, HORZRES);
ScreenHeight = GetDeviceCaps(DesktopDC, VERTRES);
Width = (ScreenWidth * Scale) / 100;
Height = (ScreenHeight * Scale) / 100;

memset(&BitmapInfo.bmiHeader, 0, sizeof(BitmapInfo.bmiHeader));
BitmapInfo.bmiHeader.biWidth = Width;
BitmapInfo.bmiHeader.biHeight = Height;
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 24;
BitmapInfo.bmiHeader.biCompression = BI_RGB;

CaptureDC = CreateCompatibleDC(DesktopDC);
Bitmap = CreateDIBSection(DesktopDC, &BitmapInfo, DIB_RGB_COLORS, (void **)&Data, 0, 0);

SelectObject(CaptureDC, Bitmap);
StretchBlt(CaptureDC, 0, 0, Width, Height, DesktopDC, 0, 0, ScreenWidth, ScreenHeight, SRCCOPY); //
1 second freeze occurs here...

Any suggestions? It seems as if StrechBlt() is freezing up due to having to convert from 32bpp to 24bpp.
I have the exact same problem when doing BitBlt() from the screen to a bitmap of similar size.
0
Comment
Question by:Olympus
  • 6
  • 2
  • 2
  • +2
13 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
This behaviour is not a big surprise - it's the sheer amount of data that has to be processed...
0
 

Author Comment

by:Olympus
Comment Utility
Yes, but this behaviour is non existant on 9x platforms. (Confirmed this morning.) Windows shouldn't be using realtime priority whilst processing the data. Is there a better way around this?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>Yes, but this behaviour is non existant on 9x platforms

You seem to forget that since NT4, the GDI has been moved into kernel space - it should work like Win9x on NT3.51 :o)
0
 
LVL 1

Expert Comment

by:senohp
Comment Utility
The code consumes large amount of memory. What is the size of your machine's available physical memory ? It seems to me that the 500ms wait is caused by memory swapping.

If you still plan use the codes in a machine with a small amount of available memory, change the code to use a multi-thread mechanisme (of course with critical section locks). In this mechanisme, your user-interface is still active (e.g. mouse & keyboard is still usable) while waiting the memory swap complete.

Regards,


Seno Hardijanto
0
 

Author Comment

by:Olympus
Comment Utility
Memory is not the concern.. i have over 250 megs free at any given time; and PLUS the code causes ZERO cpu freeze on my 98 partition.
0
 

Author Comment

by:Olympus
Comment Utility
(This is not being run on an application with a GUI.)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:senohp
Comment Utility
You're right;

I test your codes with my XP machine, there is no glitch in memory utilization, only in CPU usage (up to 50%). But in my environment there is no difference whether I use 16 bits or 32 bits screen depth, and ScretchBlt function call consumes CPU only about 100-200 ms (I use PIII 900Mhz).

Some of the GDI call were not performed by the CPU itself. The processor only relay the calls to the graphic adapter. So, I think you need to concern about the graphic adapter.
0
 

Author Comment

by:Olympus
Comment Utility
There is no easy way to do the bpp conversion better with API functions?
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
Dear Olympus

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "jkr"

comment(s) as an answer.

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

Author Comment

by:Olympus
Comment Utility
Well senohp was able to reproduce the problem; is there a way to do this with GetDIBits and StretchDIBits to get rid of the delays in BitBlt and StretchBlt?
0
 
LVL 6

Expert Comment

by:Mindphaser
Comment Utility
Please update and finalize this old, open question. Please:

1) Award points ... if you need Moderator assistance to split points, comment here with details please or advise us in Community Support with a zero point question and this question link.
2) Ask us to delete it if it has no value to you or others
3) Ask for a refund so that we can move it to our PAQ at zero points if it did not help you but may help others.

EXPERT INPUT WITH CLOSING RECOMMENDATIONS IS APPRECIATED IF ASKER DOES NOT RESPOND.

Thanks,

** Mindphaser - Community Support Moderator **

P.S.  Click your Member Profile, choose View Question History to go through all your open and locked questions to update them.
0
 

Author Comment

by:Olympus
Comment Utility
3; refund would be best.
0
 
LVL 6

Accepted Solution

by:
Mindphaser earned 0 total points
Comment Utility
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 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

9 Experts available now in Live!

Get 1:1 Help Now