Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help. Create a bmp file in C++

Help. Create a bmp file in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicshelptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Ming Luo
    wrote on last edited by
    #1

    Hi all: I tried to sort out creating a bmp file and save an image in C++. It really annoyed me. Following is my test code, but it doesn't work. Could somebody give me an idea how to create a bmp file and store an image please???? Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! void draw(){ int x[100]; int n = 0; for(int i = 0; i < 10; i ++){ for (int j = 0; j < 10; j ++){ x[i*10+j] = n++; if (n >= 255) n = 0; } } HBITMAP map = CreateBitmap(10, 10, 1, 8, x); if (map == NULL) cout << "cao ni ma\n"; HANDLE hf; // file handle BITMAPFILEHEADER hdr; // bitmap file-header BITMAPINFOHEADER bih; // bitmap info-header LPCTSTR filename = "output.bmp"; DWORD dwTmp; hdr.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(x); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfType = 0x4d42; hdr.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = 100; bih.biHeight = 100; bih.biPlanes = 1; bih.biBitCount = 8; bih.biCompression = BI_RGB; bih.biSizeImage = 0; bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biClrUsed = 0; bih.biClrImportant = 0; hf = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, (DWORD) 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL); WriteFile(hf, &hdr, sizeof(BITMAPFILEHEADER),(LPDWORD) &dwTmp, NULL); WriteFile(hf, &bih, sizeof(BITMAPINFOHEADER), (LPDWORD) &dwTmp, NULL); WriteFile(hf, (LPSTR) x, 100, (LPDWORD) &dwTmp, NULL); } Asura

    J C 2 Replies Last reply
    0
    • M Ming Luo

      Hi all: I tried to sort out creating a bmp file and save an image in C++. It really annoyed me. Following is my test code, but it doesn't work. Could somebody give me an idea how to create a bmp file and store an image please???? Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! void draw(){ int x[100]; int n = 0; for(int i = 0; i < 10; i ++){ for (int j = 0; j < 10; j ++){ x[i*10+j] = n++; if (n >= 255) n = 0; } } HBITMAP map = CreateBitmap(10, 10, 1, 8, x); if (map == NULL) cout << "cao ni ma\n"; HANDLE hf; // file handle BITMAPFILEHEADER hdr; // bitmap file-header BITMAPINFOHEADER bih; // bitmap info-header LPCTSTR filename = "output.bmp"; DWORD dwTmp; hdr.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(x); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfType = 0x4d42; hdr.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = 100; bih.biHeight = 100; bih.biPlanes = 1; bih.biBitCount = 8; bih.biCompression = BI_RGB; bih.biSizeImage = 0; bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biClrUsed = 0; bih.biClrImportant = 0; hf = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, (DWORD) 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL); WriteFile(hf, &hdr, sizeof(BITMAPFILEHEADER),(LPDWORD) &dwTmp, NULL); WriteFile(hf, &bih, sizeof(BITMAPINFOHEADER), (LPDWORD) &dwTmp, NULL); WriteFile(hf, (LPSTR) x, 100, (LPDWORD) &dwTmp, NULL); } Asura

      J Offline
      J Offline
      jinzhecheng
      wrote on last edited by
      #2

      Hello Luo ming, Take a look at "programing windows", there is intensive discussion about bitmap stuff. Good luck!

      1 Reply Last reply
      0
      • M Ming Luo

        Hi all: I tried to sort out creating a bmp file and save an image in C++. It really annoyed me. Following is my test code, but it doesn't work. Could somebody give me an idea how to create a bmp file and store an image please???? Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! void draw(){ int x[100]; int n = 0; for(int i = 0; i < 10; i ++){ for (int j = 0; j < 10; j ++){ x[i*10+j] = n++; if (n >= 255) n = 0; } } HBITMAP map = CreateBitmap(10, 10, 1, 8, x); if (map == NULL) cout << "cao ni ma\n"; HANDLE hf; // file handle BITMAPFILEHEADER hdr; // bitmap file-header BITMAPINFOHEADER bih; // bitmap info-header LPCTSTR filename = "output.bmp"; DWORD dwTmp; hdr.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(x); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfType = 0x4d42; hdr.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = 100; bih.biHeight = 100; bih.biPlanes = 1; bih.biBitCount = 8; bih.biCompression = BI_RGB; bih.biSizeImage = 0; bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biClrUsed = 0; bih.biClrImportant = 0; hf = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, (DWORD) 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL); WriteFile(hf, &hdr, sizeof(BITMAPFILEHEADER),(LPDWORD) &dwTmp, NULL); WriteFile(hf, &bih, sizeof(BITMAPINFOHEADER), (LPDWORD) &dwTmp, NULL); WriteFile(hf, (LPSTR) x, 100, (LPDWORD) &dwTmp, NULL); } Asura

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        Ming Luo wrote:

        WriteFile(hf, (LPSTR) x, 100, (LPDWORD) &dwTmp, NULL);

        for starters, for a 100x100 RGB image, you need to write 300 bytes worth of pixel data per each pixel row. looks like you're only writing 100 bytes total. Cleek | Image Toolkits | Thumbnail maker

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups