Loading error

I'm relatively new to c++.
I use the DEV-c++ compiler,and for a while I've been trying to print bitmaps on the screen. But when I use the LoadBMP() function I get an error not from my compiler, but from my desktop asking me if I want to send this error to windows(which i never do). Can anyone help me troubleshoot this or find a better way to print bitmaps.?
Last edited on
It probably means you've got a dangling pointer in there somewhere.

Post the offending code.
here:[code]

#include"load.h"
#include<windows.h>

main()
{
LoadBMP("rec/_rangertst_.bmp",0);

while (1==1);//this keeps the program from shutting off instantly
return 0;
}
[code/]

this is the "load.h" i found

[code]

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>


bool LoadBMP(char *sBMPFile,unsigned char* buffer)
{
FILE *fp;
char buf[0x36];
int i,j;
int ch;

fp=fopen(sBMPFile,"rb");
if( fp != NULL ) {
if (fread(buf,0x36, 1, fp ) !=0) {
for(i=0,j=0;i<256*256;i++,j+=3) {
buffer[j+2]=(unsigned char)fgetc(fp);
buffer[j+1]=(unsigned char)fgetc(fp);
buffer[j] =(unsigned char)fgetc(fp);
}
}
fclose( fp );
} else printf("file not found\n");

return true;
}
[code\]

when the file's not found it works perfectly
Last edited on
You are trying to write to memory you don't own. Try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <windows.h>

#include "load.h"

int main()
{
  unsigned char *p;
  int x, y;

  // pixels represents the bits of the image.
  // I assume an image sized 256 by 256 pixels (because that is
  // what the LoadBMP() function assumes).
  // The function loads pixels as triplets ordered as R,G,B.
  unsigned char *pixels = (unsigned char*)malloc( 256 * 256 * 3 );

  // Fill the pixels buffer from the file
  LoadBMP( "test256x256.bmp", pixels );

  // Draw the image on the console window

  // first, find the console window
  SetConsoleTitle( "ozone95's bitmap" );
  HWND hConsole = FindWindow( NULL, "ozone95's bitmap" );
  HDC hConsoleDC = GetDC( hConsole );

  // then, loop through all the pixels and draw them
  p = pixels;
  for (y = 255; y >= 0; y--)
    for (x = 0; x < 256; x++)
    {
      SetPixel( hConsoleDC, x, y, RGB( p[0], p[1], p[2] ) );
      p += 3;
    }

  // finally, release the DC and free memory used
  ReleaseDC( hConsole, hConsoleDC );
  DeleteDC( hConsoleDC );
  free( pixels );

  // wait for the user to end
  getchar();
  return 0;
}


You'll have to fix that 'bool' and 'true' in "load.h" to 'int' and '1' respectively. When you compile you'll have to link against libgdi32:
gcc main.c -lgdi32

Make sure that your bitmap file is a 256 by 256 pixel, 24-bit, uncompressed, unflipped DIB, or you might see some weird stuff.

BTW, the LoadBMP() function should have its own .c file.

Hope this helps.
Last edited on
Tks very much, I just have one more question how do you use
gcc main.c -lgdi32 to link libgdi32?
Sorry I haven't looked in here for so long...

That command is if you are using the GCC at the command prompt. If you are using another C compiler change the name. For example, Borland would be:
bcc main.c -lgdi32

If you are using an IDE, you should have a menu option somewhere to specify which libraries you want to link with. Link with the gdi32 library.

Hope this helps.
I am extreamly sorry for taking so long to tinker with this, but i've found how to link the library. It doesn't work just yet but i'm very close,tks!
If you want to upgrade your code from "C style", to "C++ style" here's a suggestion. I haven't tested if this works, because I don't have windows. You might have to fixe some errors, but you should get the idea.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "load.h"

#include <windows.h>

#include <iostream>
#include <fstream>
#include <cstdio>

const int W = 256;
const int H = 256;

bool LoadBMP(const char* sBMPFile, unsigned char* buffer)
{ 
    int i,j;
    
    std::ifstream file(sBMPFile, ios::in | ios::binary);
    if ( file ) {
        if (file.seekg(0x36)) {
            for (i=0,j=0; i < W*H; i++,j+=3) {
                buffer[j+2]=(unsigned char)file.get();
                buffer[j+1]=(unsigned char)file.get();
                buffer[j]  =(unsigned char)file.get();
            }
        }
    } else {
        std::cout << "file not found" << std::endl;
        return false;
    }
    return true;
}


int main()
{
    // pixels represents the bits of the image.
    // I assume an image sized 256 by 256 pixels (because that is
    // what the LoadBMP() function assumes).
    // The function loads pixels as triplets ordered as R,G,B.
    unsigned char* pixels = new unsigned char[W * H * 3];
    
    // Fill the pixels buffer from the file
    LoadBMP( "test256x256.bmp", pixels );
    
    // Draw the image on the console window
    
    // first, find the console window
    SetConsoleTitle( "ozone95's bitmap" );
    HWND hConsole = FindWindow( NULL, "ozone95's bitmap" );
    HDC hConsoleDC = GetDC( hConsole );
    
    // then, loop through all the pixels and draw them
    unsigned char* p = pixels;
    for (int y = H-1; y >= 0; y--) {
        for (int x = 0; x < W; x++) {
            SetPixel( hConsoleDC, x, y, RGB( p[0], p[1], p[2] ) );
            p += 3;
        }
    }
    
    // finally, release the DC and free memory used
    ReleaseDC( hConsole, hConsoleDC );
    DeleteDC( hConsoleDC );
    delete[] pixels;
    
    // wait for the user to end
    getchar();
    return 0;
}


Then save as main.cpp, and use g++ instead of gcc to compile.
Topic archived. No new replies allowed.