Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - CreateDIBSection, CreateDIBitmap, BitBlt, etc., etc. | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
beneficii

Lakitu
Level: 36

Posts: 135/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 06-16-05 06:06 PM Link | Quote
This is my first time attemptin the GDI API. I was able to load a bitmap from a resource and display it through this:

case WM_PAINT:{
sashi = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_BITMAP));
parentdc = BeginPaint(hwnd, &ps);
hdc = CreateCompatibleDC(parentdc);
gone = (HBITMAP) SelectObject(hdc, sashi);
GetObject(sashi, sizeof(bm), &bm);
BitBlt(parentdc, 0, 0, 299, 169, hdc, 0, 0, SRCCOPY);
SelectObject(hdc, gone);
DeleteDC(hdc);
EndPaint(hwnd, &ps);
break;}

//Many variables are elsewhere declared. parentdc and hdc are HDC, gone and sashi are HBITMAP, bm is BITMAP, and ps is PAINTSTRUCT. For all those who think every piece of code I post is wrong (this's directed to you sloat ), this code works as intended. The bitmap in the resource is successfully displayed on the window. This is not the code I have the problem with, but it merely demonstrates what I've been able to do thus far.

Now on to the code that gives me a hassle. I created a 24-bit bitmap in memory, whose pixel data is stored in an int array file. The bitmap data is fine, and I've demonstrated that it is fine by writing a function that saves it to a file. When I open up the bitmap in Microsoft Paint, it is perfectly done. Therefore, the pixel data is not the problem. The problem occurs when I try to display the pixel data in a window, therein lies the problem. I've done this piece of code (and several variations), which is based on the older one, but I still can't get it to work. Here:

case WM_PAINT:{
HDC hdc, hdcmem;
DIBSECTION bm;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
org = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void **) &bitmapdata, 0, 0);
hdcmem = CreateCompatibleDC(hdc);
SelectObject(hdcmem, org);
GetObject(org, sizeof(DIBSECTION), &bm);
BitBlt(hdc, 50, 50, 16, 16, hdcmem, 0, 0, SRCCOPY);
DeleteDC(hdcmem);
EndPaint(hwnd, &ps);
break;}

//Some stuff ain't declared here. HBITMAP org; BITMAPINFO bmi; int bitmapdata[0x300]. The bitmapdata is the pixel data btw.

Basically, when I run this, random, ever-changing stuff appears where my bitmap should be. Even if the code is unchanged from the previous execution, usually something different will be drawn anyway. When I loaded my browser to Acmlm and then ran my program, the little hills that are the Acmlm board's icon became my image. As I loaded them again and again they got more messed up. I hope you get what I mean.

I did check the Internet for tutorials on displaying DIB's, but none seemed to explain it clearly, in a way I could understand, or in a way I felt fit my situation. Perhaps y'all can explain what exactly does it take to display a DIB on a window. Thanks in advance!

EDIT: BTW, the BITMAPINFO was already set up here, in another function:

bmpi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpi->bmiHeader.biWidth = 16;
bmpi->bmiHeader.biHeight = 16;
bmpi->bmiHeader.biPlanes = 1;
bmpi->bmiHeader.biBitCount = 24;
bmpi->bmiHeader.biCompression = BI_RGB;


(edited by beneficii on 06-16-05 09:28 AM)
sloat

Level: 16

Posts: 57/85
EXP: 18044
For next: 2212

Since: 05-21-04
From: South Central Delaware

Since last post: 19 days
Last activity: 5 hours
Posted on 06-16-05 09:01 PM Link | Quote
try 32 for the biBitCount member. The int type is 32-bits by default, and if you're telling windows read it as 24-bits, that would cause some issues.

If that's not the problem, try using SetDIBitsToDevice instead of BitBlt. With SetDIBitsToDevice, you don't have to create a memory DC or select the DIB into it.

Also, be sure to delete your DIB Section or image you loaded with LoadBitmap, or else you'll end up with a huge GDI memory leak.
beneficii

Lakitu
Level: 36

Posts: 136/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 06-16-05 09:08 PM Link | Quote
Originally posted by sloat
try 32 for the biBitCount member. The int type is 32-bits by default, and if you're telling windows read it as 24-bits, that would cause some issues.

If that's not the problem, try using SetDIBitsToDevice instead of BitBlt. With SetDIBitsToDevice, you don't have to create a memory DC or select the DIB into it.

Also, be sure to delete your DIB Section or image you loaded with LoadBitmap, or else you'll end up with a huge GDI memory leak.


I don't think it being read like that is the problem. I did an unsigned char once instead of int, and it still had the same issue; it gave the same kind of screen. I think the problem is that I keep getting bad handles or something.

Thanks! I have to leave in a few minutes, and my computer has been on for a while, so I'll have to try this out in a couple hours when I get back. I think simplifying the process of displaying this bitmap as much as I can is a good idea. Thanks.

BTW, my HBITMAP gets deleted in the WM_DESTROY case in the WndProc. Thanks a bunch! I'll post or edit a reply when I get back and test it out!
sloat

Level: 16

Posts: 58/85
EXP: 18044
For next: 2212

Since: 05-21-04
From: South Central Delaware

Since last post: 19 days
Last activity: 5 hours
Posted on 06-16-05 09:24 PM Link | Quote
Oh wait, I just remembered something.
When you call CreateDIBSection, the ppvBits param gives you the pointer to the bitmap data. You'll have to use a different var and copy your bitmap data over. Your bitmapdata variable is actually getting repointed to a different array.

Edit: Even though you delete the bitmap in WM_DESTROY, you still could get a leak. WM_PAINT will get called everytime the window or part of the window needs to be repainted. So if you load the bitmap or create a DIB everytime WM_PAINT is called, you will overwrite the variable and lose the old bitmap handle. You can just have the bitmap loaded or DIB created once before you start the message loop, or in WM_CREATE or something.


(edited by sloat on 06-16-05 12:29 PM)
beneficii

Lakitu
Level: 36

Posts: 137/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 06-16-05 11:52 PM Link | Quote
sloat,

Well, I tried the function, but the exact same effect it had:

case WM_PAINT:{
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
SetDIBitsToDevice(hdc, 50, 50, 16, 16, 0, 0, 0, 16, bitmapdata, &bmi, DIB_RGB_COLORS);
DeleteDC(hdc);
EndPaint(hwnd, &ps);
break;}

I think I did it right. Do you have any corrections?
sloat

Level: 16

Posts: 59/85
EXP: 18044
For next: 2212

Since: 05-21-04
From: South Central Delaware

Since last post: 19 days
Last activity: 5 hours
Posted on 06-17-05 03:27 AM Link | Quote
take the DeleteDC call out.
Other than that, the code worked for me. The only difference was that I used a 32-bit bitmap because i didn't feel like fumbling with 24-bits.
beneficii

Lakitu
Level: 36

Posts: 138/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 06-17-05 05:55 AM Link | Quote
Originally posted by sloat
take the DeleteDC call out.
Other than that, the code worked for me. The only difference was that I used a 32-bit bitmap because i didn't feel like fumbling with 24-bits.


It still doesn't work, even with the bitmap being set to 32-bit and DeleteDC taken out. Here's what I have:

case WM_PAINT:{
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
SetDIBitsToDevice(hdc, 50, 50, 16, 16, 0, 0, 0, 16, bitmapdata, &bmi, 0);
EndPaint(hwnd, &ps);
break;}

BTW, the 32-bitmap displays fine in Paint (I didn't know it could show 32-bit bitmaps, but apparently it can), even when I have a display setting lower than 32-bit color. And even when I have the display setting at 32-bit color, it still doesn't show in the program. An image of what I keep getting is linked below (btw what I keep getting now seems to be constant, which I guess is an improvement). The exported bitmap is also linked.

http://www.freewebs.com/beneficii/thetitleofmywindow.png




(edited by beneficii on 06-16-05 08:56 PM)
(edited by beneficii on 06-16-05 09:17 PM)
(edited by beneficii on 06-17-05 03:07 AM)
sloat

Level: 16

Posts: 60/85
EXP: 18044
For next: 2212

Since: 05-21-04
From: South Central Delaware

Since last post: 19 days
Last activity: 5 hours
Posted on 06-17-05 07:10 AM Link | Quote
This might make things easier. I wrote a little example that displays the bitmap data using the two methods CreateDIBSection and SetDIBitsToDevice.

I used the bitmap that you uploaded and I exported the raw data through hex-workshop. I also removed the header so that it's just the RGB values.

Here's a link...hope it clears things up.

http://sloat.liek.us/?view=file&id=16
beneficii

Lakitu
Level: 36

Posts: 139/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 06-17-05 07:55 AM Link | Quote
Your program crashes my computer, but I saw something you did in your program that I didn't do in mine. I implemented it in mine, and guess what, it works! Success! Success!

What I didn't do was arrange the pixel array correctly. So I converted it to an unsigned char from an int, and when I ran it, it worked!

Thanks, man!
BlueSonic

Micro-Goomba
Level: 7

Posts: 7/19
EXP: 951
For next: 497

Since: 06-23-05
From: Finland

Since last post: 113 days
Last activity: 112 days
Posted on 06-24-05 03:21 PM Link | Quote
I might have a use for this code too. I've always used SetBitmapBits() and GetBitmapBits() when I wanted to put an array into bitmap... but I guess DIBSection is faster.
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - CreateDIBSection, CreateDIBitmap, BitBlt, etc., etc. | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.023 seconds.