You are looking for something like the
file command on Linux with it's
magic data file.
If you only want to identify a few image file types, just read some bytes from the begin of the file and check them for image file specific sequences:
const char *lpszType = NULL;
char pBuffer[16] = "";
FILE *f = fopen(lpszFile, "rb");
if (f)
{
fread(pBuffer, 1, 16, f);
if (0 == memcmp(pBuffer, "BM", 2) && 0 == memcmp(pBuffer + 6, "\0\0\0", 4))
lpszType = "BMP";
else if (0 == strcmp(pBuffer, "II*") || 0 == strcmp(pBuffer, "MM*"))
lpszType = "TIFF";
else if (0 == memcmp(pBuffer, "\x89PNG\r\n\x1A\n", 8))
lpszType = "PNG";
else if (0 == memcmp(pBuffer, "GIF87a", 6) ||
0 == memcmp(pBuffer, "GIF89a", 6))
lpszType = "GIF";
else if (0 == memcmp(pBuffer, "\xFF\xD8\xFF", 3) &&
pBuffer[3] >= 0xE0 && pBuffer[3] <= 0xEF &&
4 == strlen(pBuffer + 6))
lpszType = "JPEG";
fclose(f);
}