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. Loading a bitmap from a *.BMP file

Loading a bitmap from a *.BMP file

Scheduled Pinned Locked Moved C / C++ / MFC
helpgraphicslearning
10 Posts 4 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.
  • T Offline
    T Offline
    TheDelChop
    wrote on last edited by
    #1

    Hello, I am trying to load a bitmap into one of my dialog boxes in the program I am coding. I have found much help in this area provided that the file I am going to load is already defined as a resource. However, my problem is that the *.BMP that I have to load will be selected by the user. I was wondering if anyone could give me a little instruction on how I am able to take a Bitmap from a *.BMP whose properities I am unaware of and get it to appear in the dialog box. Thank you, Joe

    C H 2 Replies Last reply
    0
    • T TheDelChop

      Hello, I am trying to load a bitmap into one of my dialog boxes in the program I am coding. I have found much help in this area provided that the file I am going to load is already defined as a resource. However, my problem is that the *.BMP that I have to load will be selected by the user. I was wondering if anyone could give me a little instruction on how I am able to take a Bitmap from a *.BMP whose properities I am unaware of and get it to appear in the dialog box. Thank you, Joe

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

      take a look at ::LoadImage

      image processing | blogging

      T 1 Reply Last reply
      0
      • C Chris Losinger

        take a look at ::LoadImage

        image processing | blogging

        T Offline
        T Offline
        TheDelChop
        wrote on last edited by
        #3

        Yes I understand that Load Image is the function I am going to be using, however, it returns a HANDLE, can I cast that as an HBITMAP and just move on?

        C 1 Reply Last reply
        0
        • T TheDelChop

          Yes I understand that Load Image is the function I am going to be using, however, it returns a HANDLE, can I cast that as an HBITMAP and just move on?

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

          yep. it's a HANDLE because you can load cursors and icons with that function, too. but, if you're using the IMAGE_BITMAP flag, you get a HANDLE to a bitmap (aka HBITMAP) back.

          image processing | blogging

          T 1 Reply Last reply
          0
          • C Chris Losinger

            yep. it's a HANDLE because you can load cursors and icons with that function, too. but, if you're using the IMAGE_BITMAP flag, you get a HANDLE to a bitmap (aka HBITMAP) back.

            image processing | blogging

            T Offline
            T Offline
            TheDelChop
            wrote on last edited by
            #5

            Ok but when I load a BMP from a file, it has to be a DIB? Could you possibly point out what doesn't seem to work here? BOOL LoadBitmapFromBMPFile( LPTSTR szFileName, HBITMAP hBitmap,HPALETTE hPalette ) { BITMAP bm; //*phBitmap = NULL; //*phPalette = NULL; // Use LoadImage() to get the image loaded into a DIBSection hBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE ); if (hBitmap == NULL ) return FALSE; // Get the color depth of the DIBSection GetObject(hBitmap, sizeof(BITMAP), &bm ); // If the DIBSection is 256 color or less, it has a color table if (( bm.bmBitsPixel * bm.bmPlanes ) <= 8 ) { HDC hMemDC; HBITMAP hOldBitmap; RGBQUAD rgb[256]; LPLOGPALETTE pLogPal; WORD i; // Create a memory DC and select the DIBSection into it hMemDC = CreateCompatibleDC( NULL ); hOldBitmap = (HBITMAP)SelectObject( hMemDC, hBitmap ); // Get the DIBSection's color table GetDIBColorTable( hMemDC, 0, 256, rgb ); // Create a palette from the color tabl pLogPal = (LOGPALETTE *)malloc( sizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY)) ); pLogPal->palVersion = 0x300; pLogPal->palNumEntries = 256; for (i = 0 ; i < 256 ; i++) { pLogPal->palPalEntry[i].peRed = rgb[i].rgbRed; pLogPal->palPalEntry[i].peGreen = rgb[i].rgbGreen; pLogPal->palPalEntry[i].peBlue = rgb[i].rgbBlue; pLogPal->palPalEntry[i].peFlags = 0; } hPalette = CreatePalette( pLogPal ); // Clean up free(pLogPal); SelectObject(hMemDC, hOldBitmap); DeleteDC(hMemDC); } else // It has no color table, so use a halftone palette { HDC hRefDC; hRefDC = GetDC( NULL ); hPalette = CreateHalftonePalette( hRefDC ); ReleaseDC( NULL, hRefDC ); } return TRUE; } Thanks a bunch in advance, Joe

            C N 2 Replies Last reply
            0
            • T TheDelChop

              Ok but when I load a BMP from a file, it has to be a DIB? Could you possibly point out what doesn't seem to work here? BOOL LoadBitmapFromBMPFile( LPTSTR szFileName, HBITMAP hBitmap,HPALETTE hPalette ) { BITMAP bm; //*phBitmap = NULL; //*phPalette = NULL; // Use LoadImage() to get the image loaded into a DIBSection hBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE ); if (hBitmap == NULL ) return FALSE; // Get the color depth of the DIBSection GetObject(hBitmap, sizeof(BITMAP), &bm ); // If the DIBSection is 256 color or less, it has a color table if (( bm.bmBitsPixel * bm.bmPlanes ) <= 8 ) { HDC hMemDC; HBITMAP hOldBitmap; RGBQUAD rgb[256]; LPLOGPALETTE pLogPal; WORD i; // Create a memory DC and select the DIBSection into it hMemDC = CreateCompatibleDC( NULL ); hOldBitmap = (HBITMAP)SelectObject( hMemDC, hBitmap ); // Get the DIBSection's color table GetDIBColorTable( hMemDC, 0, 256, rgb ); // Create a palette from the color tabl pLogPal = (LOGPALETTE *)malloc( sizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY)) ); pLogPal->palVersion = 0x300; pLogPal->palNumEntries = 256; for (i = 0 ; i < 256 ; i++) { pLogPal->palPalEntry[i].peRed = rgb[i].rgbRed; pLogPal->palPalEntry[i].peGreen = rgb[i].rgbGreen; pLogPal->palPalEntry[i].peBlue = rgb[i].rgbBlue; pLogPal->palPalEntry[i].peFlags = 0; } hPalette = CreatePalette( pLogPal ); // Clean up free(pLogPal); SelectObject(hMemDC, hOldBitmap); DeleteDC(hMemDC); } else // It has no color table, so use a halftone palette { HDC hRefDC; hRefDC = GetDC( NULL ); hPalette = CreateHalftonePalette( hRefDC ); ReleaseDC( NULL, hRefDC ); } return TRUE; } Thanks a bunch in advance, Joe

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

              TheDelChop wrote:

              Ok but when I load a BMP from a file, it has to be a DIB?

              nope. you can read it to a 'normal' HBITMAP.

              TheDelChop wrote:

              Could you possibly point out what doesn't seem to work here?

              the hBitmap and hPalette parameters should probably be references or pointers, if you want to set them so the calling function can use them.

              image processing | blogging

              T 1 Reply Last reply
              0
              • C Chris Losinger

                TheDelChop wrote:

                Ok but when I load a BMP from a file, it has to be a DIB?

                nope. you can read it to a 'normal' HBITMAP.

                TheDelChop wrote:

                Could you possibly point out what doesn't seem to work here?

                the hBitmap and hPalette parameters should probably be references or pointers, if you want to set them so the calling function can use them.

                image processing | blogging

                T Offline
                T Offline
                TheDelChop
                wrote on last edited by
                #7

                Assuming that the resourse IDB_BITMAP1 is stored in C:\BITMAP1.BMP, any difference on what these two functions will return? hbm = (HBITMAP)LoadImage(NULL,"C:\BITMAP1.BMP",IMAGE_BITMAP,0,0, LR_LOADFROMFILE); hbm = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP1));

                C 1 Reply Last reply
                0
                • T TheDelChop

                  Assuming that the resourse IDB_BITMAP1 is stored in C:\BITMAP1.BMP, any difference on what these two functions will return? hbm = (HBITMAP)LoadImage(NULL,"C:\BITMAP1.BMP",IMAGE_BITMAP,0,0, LR_LOADFROMFILE); hbm = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP1));

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

                  should be the same thing

                  image processing | blogging

                  1 Reply Last reply
                  0
                  • T TheDelChop

                    Ok but when I load a BMP from a file, it has to be a DIB? Could you possibly point out what doesn't seem to work here? BOOL LoadBitmapFromBMPFile( LPTSTR szFileName, HBITMAP hBitmap,HPALETTE hPalette ) { BITMAP bm; //*phBitmap = NULL; //*phPalette = NULL; // Use LoadImage() to get the image loaded into a DIBSection hBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE ); if (hBitmap == NULL ) return FALSE; // Get the color depth of the DIBSection GetObject(hBitmap, sizeof(BITMAP), &bm ); // If the DIBSection is 256 color or less, it has a color table if (( bm.bmBitsPixel * bm.bmPlanes ) <= 8 ) { HDC hMemDC; HBITMAP hOldBitmap; RGBQUAD rgb[256]; LPLOGPALETTE pLogPal; WORD i; // Create a memory DC and select the DIBSection into it hMemDC = CreateCompatibleDC( NULL ); hOldBitmap = (HBITMAP)SelectObject( hMemDC, hBitmap ); // Get the DIBSection's color table GetDIBColorTable( hMemDC, 0, 256, rgb ); // Create a palette from the color tabl pLogPal = (LOGPALETTE *)malloc( sizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY)) ); pLogPal->palVersion = 0x300; pLogPal->palNumEntries = 256; for (i = 0 ; i < 256 ; i++) { pLogPal->palPalEntry[i].peRed = rgb[i].rgbRed; pLogPal->palPalEntry[i].peGreen = rgb[i].rgbGreen; pLogPal->palPalEntry[i].peBlue = rgb[i].rgbBlue; pLogPal->palPalEntry[i].peFlags = 0; } hPalette = CreatePalette( pLogPal ); // Clean up free(pLogPal); SelectObject(hMemDC, hOldBitmap); DeleteDC(hMemDC); } else // It has no color table, so use a halftone palette { HDC hRefDC; hRefDC = GetDC( NULL ); hPalette = CreateHalftonePalette( hRefDC ); ReleaseDC( NULL, hRefDC ); } return TRUE; } Thanks a bunch in advance, Joe

                    N Offline
                    N Offline
                    nutkase
                    wrote on last edited by
                    #9

                    The returned handle is ALWAYS a DDB if you dont set the flag LR_CREATEDIBSECTION and a DIB if u do set the flag. So, if your working on 24+ bits per pixel display you dont even have to worry abou the palette and other stuff.. just load it and enjoy :cool: Peace!

                    1 Reply Last reply
                    0
                    • T TheDelChop

                      Hello, I am trying to load a bitmap into one of my dialog boxes in the program I am coding. I have found much help in this area provided that the file I am going to load is already defined as a resource. However, my problem is that the *.BMP that I have to load will be selected by the user. I was wondering if anyone could give me a little instruction on how I am able to take a Bitmap from a *.BMP whose properities I am unaware of and get it to appear in the dialog box. Thank you, Joe

                      H Offline
                      H Offline
                      Hamid Taebi
                      wrote on last edited by
                      #10

                      If you want to load file you can use LoadImage or CImage class


                      WhiteSky


                      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