473,769 Members | 3,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Displaying .bmps on screen + BitBlt problem

93 New Member
Hi,
I want to run an animation(in essence). I want to draw multiple images (possibly .bmps, but I'd like other formats as well.) on to the screen each frame.
I tried to do this with some C++ code, but it isn't working as it should (As I'm expecting it to I guess.). Can anyone please help me?

Here's the class definition. Just for the variables.
Expand|Select|Wrap|Line Numbers
  1. class CWorld
  2. {
  3. private:
  4.  
  5.     //Window DC
  6.     HDC windowDC;
  7.     //This is the backbuffer. We incrementally draw onto this and paste to the actual window DC once. This reduces flicker.
  8.     HDC bufferDC;
  9.     //This is a utility DC that will be used to load bitmaps into and pasting them to the back buffer.
  10.     HDC memDC;
  11.  
  12.     //The bitmap for the buffer.
  13.     HBITMAP hBufferBitMap;
  14.  
  15. public:
  16.     BOOL DrawFrame();
  17.  
  18.     CWorld(HDC windowDC);
  19.     ~CWorld(void);
  20. };
  21.  
ctor.
Expand|Select|Wrap|Line Numbers
  1. CWorld::CWorld(HDC windowDC)
  2. {
  3.     this->windowDC = windowDC;
  4.  
  5.     this->bufferDC = NULL;
  6.     this->bufferDC = ::CreateCompatibleDC(windowDC);
  7.  
  8.     this->memDC = NULL;
  9.     this->memDC = ::CreateCompatibleDC(this->bufferDC);
  10.  
  11.     HBITMAP hbmBuffer = ::CreateCompatibleBitmap(    this->bufferDC,
  12.                                                     ::GetDeviceCaps(this->bufferDC, HORZRES),
  13.                                                     ::GetDeviceCaps(this->bufferDC, VERTRES));
  14.     HBITMAP h = (HBITMAP)::SelectObject(this->bufferDC, hbmBuffer);
  15.  
  16.     //Check for errors.
  17. }
  18.  

The drawing code.
Expand|Select|Wrap|Line Numbers
  1. BOOL CWorld::DrawFrame()
  2. {
  3.     static HBITMAP testBmp = (HBITMAP)::LoadImageA(NULL, "C:\\dota.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  4.  
  5.     HBITMAP memOld = (HBITMAP)::SelectObject(this->memDC, testBmp);
  6.  
  7.     //A white background.
  8.     ::PatBlt(    this->bufferDC, 0, 0, 
  9.                 GetDeviceCaps(this->bufferDC, HORZRES), GetDeviceCaps(this->bufferDC, VERTRES),
  10.                 WHITENESS);
  11.  
  12.     //BitBlt the .bmp onto the back buffer.
  13.     ::BitBlt(    this->bufferDC, 0, 0,
  14.                 GetDeviceCaps(this->bufferDC, HORZRES), GetDeviceCaps(this->bufferDC, VERTRES),
  15.                 this->memDC, 0, 0,
  16.                 SRCCOPY);
  17.  
  18.     //BitBlt the back buffer to the window DC.
  19.     ::BitBlt(    this->windowDC, 0, 0,
  20.                 GetDeviceCaps(this->windowDC, HORZRES), GetDeviceCaps(this->windowDC, VERTRES),
  21.                 this->bufferDC, 0, 0,
  22.                 SRCCOPY);
  23.  
  24.  
  25.     ::SelectObject(this->memDC, memOld);
  26.  
  27.     return TRUE;
  28. }
  29.  
I fiddled around with the code for sometime but couldn't get it done.

I have couple of questions on this. I'd be grateful if anyone can help me.

1. Why doesn't my code show the .bmp on the screen?(Of course) All I see is a white window.

2. As you may have noticed, I used 3 DCs. One is the actual DC of the window.
I have a DC, bufferDC which I use for drawing behind. The other, memDC is just something I used for loading the bmp onto. I wouldn't have needed this if I only wanted to paint one image. But, as I want to draw more than one image, I thought I needed this to load the bmp into, and then to paste it onto the bufferDC. Is this the way to do this? Or is there a better way?

3. I saw somewhere that HBITMAPs use OS resources. That sounds bad. So, if I want lots of images in my app, would that create a problem? If so, is there any way around it? Without of course, using OpenGL or some lib.

Thanks!
Apr 4 '08 #1
2 2862
IanWright
179 New Member
Hi DumRat,

I'm actually working on similar types of stuff at the moment for the first time. Having never really done any windows handle based drawing I'm trying to understand it and have now decided to try and replicate the functionality with some simple cross-platform graphics libraries.

Now unfortunately I'm not good enough to tell you where your program is wrong, but can I suggest that you:

a) Check the bufferDC bits to see if you've managed to get any data into it.
b) Try setting the colour of a pixel, or drawing a primative (e.g. line, point) to your display handle to check that its working...

In answer to question 3, as that is almost exactly what I'm working on, one way of improving performance is through tiling the images. Once tiled you can then store them in memory, or to files and through caching can actually obtain pretty decent results.
Apr 4 '08 #2
Studlyami
464 Recognized Expert Contributor
Heres a couple suggestions (please note i haven't done WIN32 in a while). I don't know why you need to draw multiple bitmaps at one time. The animation should only display 1 image a frame (unless your trying transparent bitmaps, but thats another topic). Then at a certain rate (what fps you want the animation to run) you will replace that image with the next one in the animation.

First i would load the bitmaps into memory (unless your talking about 100's of bitmaps) something like: HBITMAP AnimationBitmap s[10]; Then i would load the bitmaps into the array. You will need a timer to run on the system and replace the bitmap on the screen with the next one in the AnimationBitmap array.

Also try what Ian said. Try drawing simple shapes to the MemDC, then try drawing just 1 bitmap. If those work, then work on getting the animation working.

Is there a reason why you are doing this in WIN32 or can you use other libraries?
Apr 4 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
13531
by: Me Mine | last post by:
i, I have trying to code a small console app that will allow a user to select a window and then create a screen capture of the window. I haven't been able to figure out how to do the screen capture part. The code is as follows, the commented out lines at the end are things I have tried, but don't work. Whenever I try to create a compatibe Bitmap or DC it comes back with an error as win32ui: CreateCompatibleDC failed
8
3467
by: gregory_may | last post by:
Is there a way to grab a "Screen Shot" that includes "Tool Tips"? I saw this code someplace, cant remember where. But it doesnt grab "Tool Tips". Is there a better way to do this in .net? Thanks! Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer
1
2491
by: Johan | last post by:
Hello, I'm trying to get a part of the regular screen onto my own dialog box.... Does anyone know how to do this?? (I guess i have to BitBlt something to something (HDC handle; handle= GetDC(NULL); for screen?? and then??) Thanks for ANY help (tried to do this all day now!)
1
2962
by: xc | last post by:
Greetings. I encountered a wield problem when grabing screen images. Sometimes in some computers I can capture the screen, but other times not so. In some computer I cannot capture the screen image, but when I change its color bits, say from 32bits to 24 or 16bits, the program can capture the image. Can anyone kindly examine my following code, and point out the flaw:
2
1428
by: Wayne | last post by:
In a Delphi application I would use GetDC passing the handle of the window I wanted to capture, then with the handle and a caption I would be able to convert the image to a bitmap. I would like to be able to do this in C#/.net, but I don't know where to start. Can someone point me in the right direction? Thanks Wayne
9
2823
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am facing. Please please help me to solve this as soon as possible. So here we go ... I am not able to take the screen shot of the windows form based "Smart
4
2101
by: fong01 | last post by:
Hi, Below source code I have try on Win 2k, XP & 2003 is work, but I try on win 98 & ME the screen is capture black screen. Dim dInt_dwRop As Integer = &HCC0020 Dim dImg As New System.Drawing.Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb565)
2
2219
by: Eddie Dunn | last post by:
I have one here that I cannot find anything on in my searching. I am implementing a screen capture functionality into my Visual Basic ..NET application. The code I am using calls the bitblt function in gdi32.dll to achieve its magic as follows: Public Function CreateScreenshot() As Bitmap
4
1411
by: Steven | last post by:
Hi, I want to create an image that looks exactly the same as the things I see in my application. I have an application in which you can create some controls, drag them anywhere you like. Now I want to export these controls to an image. The image should reflect the position of the controls and the controls should look the same in the picture as they do on the screen.
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.