Advertisement

What's wrong with this?

Started by November 12, 2000 02:39 PM
2 comments, last by vbisme 24 years, 1 month ago
The following, if done right should open a bitmap file, create a new file and write what''s in the old file to the new one:
  
#include <fstream.h>
#include <windows.h>
#include <wingdi.h>

typedef struct BitmapFileStructure
{
	BITMAPFILEHEADER	BitmapFileHeader;
	BITMAPINFOHEADER	BitmapInfoHeader;
	UCHAR				*Buffer;
}BitmapFile, *BitmapFilePointer;

int main()
{
	//HBITMAP			hBitmap;			//handle to bitmap

	BitmapFilePointer	myBitmap = NULL;	//hold the bitmap file

	HANDLE				hFileBmp;			//handle to bmp file

	HANDLE				hFileGfx;			//handle to custom file

	LPDWORD				NumBytesWritten, NumBytesRead;

	///open bitmap file

	hFileBmp = CreateFile("bugs.bmp", GENERIC_READ, 0,
		NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	///open custom file

	hFileGfx = CreateFile("game.bmp", GENERIC_WRITE, 0,
		NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	///read the fileheader

	ReadFile(hFileBmp, &myBitmap->BitmapFileHeader, sizeof(BITMAPFILEHEADER), NumBytesRead, NULL);
	///read the bitmapinfoheader

	ReadFile(hFileBmp, &myBitmap->BitmapInfoHeader, sizeof(BITMAPINFOHEADER), NumBytesRead, NULL);
	///Set the file pointer to read at the actually bitmap data

	SetFilePointer(hFileBmp, -(myBitmap->BitmapInfoHeader.biSize), NULL, FILE_END);
	///Read the actual bitmap data

	if(myBitmap->Buffer) { free(myBitmap->Buffer); }
	if (!(myBitmap->Buffer = (UCHAR *)malloc(myBitmap->BitmapInfoHeader.biSizeImage)))
	{
		CloseHandle(hFileBmp);
		return(0);
	}//end if

	ReadFile(hFileBmp, &myBitmap->Buffer, sizeof(myBitmap->BitmapInfoHeader.biSizeImage), NumBytesRead, NULL);

	///write the bitmap to the custom created file

	SetFilePointer(hFileGfx, 0, NULL, FILE_BEGIN);
	WriteFile(hFileGfx, &myBitmap->BitmapFileHeader , sizeof(BITMAPFILEHEADER), NumBytesWritten, NULL);
	WriteFile(hFileGfx, &myBitmap->BitmapInfoHeader , sizeof(BITMAPINFOHEADER), NumBytesWritten, NULL);
	WriteFile(hFileGfx, &myBitmap->Buffer , sizeof(myBitmap->BitmapInfoHeader.biSizeImage), NumBytesWritten, NULL);
	
	if (myBitmap->Buffer) { free(myBitmap->Buffer); }
	
	CloseHandle(hFileBmp);
	CloseHandle(hFileGfx);

	return(0);
}
  
The problem exist during the ReadFile() functions. Nothing was read. Please Help!!!!!!!!
Check the return value on your ReadFile() call. If it returns false, call GetLastError and find out what error value was returned.
Advertisement
I''m not quite sure but I remember a thread a while ago saying that some programs won''t write the correct size in the bitmap but only 0.

So maybe you should check if the value is 0 and, if it is, compute it (width * height * (bit depth / 8)).

Hope this helps.
The error returned by GetLastError() is "120"

120 This function is not supported on this system. ERROR_CALL_NOT_IMPLEMENTED



Edited by - vbisme on November 12, 2000 11:05:24 PM

This topic is closed to new replies.

Advertisement