![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 8
Rep Power: 0
![]() |
Hi.
This is not exactly a C++ question, but I encountered this problem while working with MS VC++, so I'll post it here. I was following a programming tutorial from Sams Teach Yourself Game Programming in 24 Hours, and there was a problem with Hour 5. In this hour, a Bitmap class is developed to be used in latter projects, and it's only supposed to deal with 256 color bitmaps. Than, 'Slideshow' app is developed, to demonstrate functionality of the Bitmap class. Here was the problem. Three of the bitmaps are supposed to be loaded from a file. At first, I couldn't get this to work. My OS is Widows 2000, and I used simple Paint program to save some pictures as 256 color bitmaps. These images wouldn’t open properly. Eventually, I tried to use bitmaps from the example CD, and they worked fine. Then I used my bitmaps with the Slideshow app from the CD, and they didn't work. At the end, I managed to fix the problem by resaving these images from older version of MS Photo Editor. So, anyone knows what's all this about? Is there any difference between old and new 256 col DIBs? Maybe someone knows how to change the code to work with both? Thanks in advance. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
You can rightclick>properties>summary on a bitmap and compare too see if there are any differences between a working and non-working bmp. I know photoshop 5/6 didnt save bitmaps properly as it didnt set the size field in the header (it only set the width + height, had problem with DIBs myself 'bout a couple of years ago), the same thing could be happenning with paint. Only way I can think of solving it would be to load them in yourself (if you've only just started learning may not be an option), or just stick with the program that works
. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2005
Posts: 8
Rep Power: 0
![]() |
Thanks.
As for properties>summary, there's no visible difference. And you're right, I just started with GDI, but I'm a fast learner. I also had the same problem with Photoshop 7. Must be some messy work from Microsoft's programmers ( ).One more question. There are three more bitmaps that are loaded as resources. There were some problems, too. I taught it was the same thing as with file import, but no. When the image is loaded, its pixels aren't at proper positions (look at the picture). Also note those strange pixels at the bottom line. I really have no idea how to fix this. Here's the code. (As far as I can tell, there's nothing wrong with it, except that it's maybe in old style. I'm using a downloaded book I recently found, but the edition dates from December 2002) //----------------------- // Create() method (Bitmap class), resource loading version // (It is called by the corresponding constructor) BOOL Bitmap::Create(HDC hDC, UINT uiResID, HINSTANCE hInstance) { // Free any previous DIB info Free(); // Find the bitmap resource HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(uiResID), RT_BITMAP); if (hResInfo == NULL) return FALSE; // Load the bitmap resource HGLOBAL hMemBitmap = LoadResource(hInstance, hResInfo); if (hMemBitmap == NULL) return FALSE; // Lock the resource and access the entire bitmap image PBYTE pBitmapImage = (BYTE*)LockResource(hMemBitmap); if (pBitmapImage == NULL) { FreeResource(hMemBitmap); return FALSE; } // Store the width and height of the bitmap BITMAPINFO* pBitmapInfo = (BITMAPINFO*)pBitmapImage; m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth; m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight; // Get a handle to the bitmap and copy the image bits PBYTE pBitmapBits; m_hBitmap = CreateDIBSection(hDC, pBitmapInfo, DIB_RGB_COLORS, (PVOID*)&pBitmapBits, NULL, 0); if ((m_hBitmap != NULL) && (pBitmapBits != NULL)) { const PBYTE pTempBits = pBitmapImage + pBitmapInfo->bmiHeader.biSize + pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD); CopyMemory(pBitmapBits, pTempBits, pBitmapInfo->bmiHeader.biSizeImage); // Unlock and free the bitmap graphics object UnlockResource(hMemBitmap); FreeResource(hMemBitmap); return TRUE; } // Something went wrong, so cleanup everything UnlockResource(hMemBitmap); FreeResource(hMemBitmap); Free(); return FALSE; } //----------------------- |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
Can't really say what's wrong from looking at the function. I never really got the whole DIBsection stuff just normally botched it 'till it worked
.const PBYTE pTempBits = pBitmapImage + pBitmapInfo->bmiHeader.biSize + pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD); Maybe this line is wrong, why are you skipping over the colour table if there is one, surely you need to copy that as well. Is there any reason your not using LoadImage() ? //load a resource hbitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP) ,IMAGE_BITMAP,0,0,LR_SHARED); //load a file hbitmap = (HBITMAP)LoadImage(0,pFilePath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE); |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
@thePaleontologist: use code tags next time
![]() |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Sep 2005
Posts: 8
Rep Power: 0
![]() |
Thanks (to both) for the advice.
Animatronic: 'Is there any reason your not using LoadImage() ?' Well, I'm fine with that (as far as I'm concerned), but the book insists on this approach. Polyphemus: '@thePaleontologist: use code tags next time ' Sorry, I didn’t' see there was such an option until Animatronic posted his answer. Maybe moderators can modify my post, since I can't edit it anymore. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|