• 13
• 18
• 19
• 27
• 9

# Copying Bitmap To Surface Without GDI

This topic is 4558 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, So I think I managed to load a 16-bit bitmap correctly using the following code:
bool CBitmap::Load(char *szFilename)
{
unsigned char* pucPixels;

char *szPath = new char[MAX_PATH];
GetModuleFileName(NULL, szPath, MAX_PATH);
PathRemoveFileSpec(szPath);

char *szFullPath = new char[strlen(szPath) + strlen(szFilename) + 2];
strcpy(szFullPath, szPath);
strcat(szFullPath, "\\");
strcat(szFullPath, szFilename);

delete[] szPath;

std::ifstream isBitmap(szFullPath, std::ios::in | std::ios::binary);
delete[] szFullPath;

if (!isBitmap.is_open())
{
(CError::GetInstance())->Notify("Failed to open bitmap for read.", "!isBitmap.is_open()");
return false;
}

{
(CError::GetInstance())->Notify("Invalid Bitmap", "bfType != Valid Bitmap");
isBitmap.close();
return false;
}

{
(CError::GetInstance())->Notify("Invalid Bitmap Depth.", "Bitmap is not 16-bit.");
isBitmap.close();
return false;
}

{
(CError::GetInstance())->Notify("Not enough memory to load bitmap.", "Failed to allocate pucPixels.");
isBitmap.close();
return false;
}

isBitmap.close();

for (int nIndex = 0; nIndex < static_cast<int>(bmpHeader.bfSize); nIndex += 3)
{
unsigned char ucTempRGB = pucPixels[nIndex];
pucPixels[nIndex] = pucPixels[nIndex + 2];
pucPixels[nIndex + 2] = ucTempRGB;
}

m_pcPixels = pucPixels;
return true;
}


Right? Well how, now, do I copy those pixels onto a surface? Help is much appreciated, thank you :) I used something such as this just to test out this method...
bool CBitmap::Draw(unsigned char *pcDest)
{
unsigned char *pcPixels = m_pcPixels;
for (int x = 0; x < m_nWidth; x++)
{
for (int y = 0; y < m_nHeight; y++)
{
pcDest[y] = pcPixels[y];
}

pcPixels += m_nWidth;
pcDest += m_nWidth;
}

return true;
}


And also, when I quit the program, I get an error when I attempt to delete[] m_pcPixels. I can't figure out why...

##### Share on other sites
Be sure to invert the bitmap (most bitmaps are writen upside-down) and consider the "padding" bits thing.

Example: if you have a 250x250 bitmap, it's writen 256x250, because the weight must be a product of 2 or something like that. There's a tutorial here on gamedev about loading bitmaps, it should help you out.

	// lock	surface->LockRect(&lockedRect, NULL, D3DLOCK_DISCARD)	// get pointer	unsigned char * pcDest = static_cast<unsigned char*>(lockedRect.pBits);	// everthing else is alike	unsigned char *pcPixels = m_pcPixels;	for (int x = 0; x < m_nWidth; x++)	{		for (int y = 0; y < m_nHeight; y++)		{			pcDest[y * lockedRect.Pitch + x * 4 + 0] = (pcPixels[y * m_nWidth + x * 3 + 0]);			pcDest[y * lockedRect.Pitch + x * 4 + 1] = (pcPixels[y * m_nWidth + x * 3 + 1]);			pcDest[y * lockedRect.Pitch + x * 4 + 2] = (pcPixels[y * m_nWidth + x * 3 + 2]);			pcDest[y * lockedRect.Pitch + x * 4 + 3] = 255;		}	}	// unlock	surface->UnlockRect(0);

if you want a "color key", just check the three values and set the fourth to 255 (opaque) or 0 (transparent) accordingly.

See ya