Loading a bmp from a string?

This is a discussion on Loading a bmp from a string? within the Windows Programming forums, part of the Platform Specific Boards category; Hey, I've been messing with bitmaps for a day or so now, and it gets very montonous to add bitmap ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Loading a bmp from a string?

    Hey, I've been messing with bitmaps for a day or so now, and it gets very montonous to add bitmap names to the resource file. I was wondering if theres some way to copy the bitmap contents to a constant string, then load it? That way theres no need for resources or external files.

    Thanks!

    (oddly enough this doesnt work:

    Code:
    const char TESTBITMAP1[] = "*Bitmap information*"
       ScreenDisplay_Object = (HBITMAP)LoadImage(GetModuleHandle(NULL), TESTBITMAP1, IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
       if(ScreenDisplay_Object == NULL)
        MessageBox(hwnd, "Could not load object!", "Error", MB_OK | MB_ICONEXCLAMATION);
    Theres more code to it, but it shouldent turn out as null, but it always does?)
    Last edited by Blackroot; 09-01-2006 at 11:54 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You could statically encode a BITMAP structure and CreateBitmapIndirect for the handle. But why do you care if there are resources?

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I dont really, I just like keeping my options open. Its also quicker for me to just copy the bitmap to a constant char rather than a res file & definition.

    It says on MSDN CreateBitmapIndirect should be used for monochrome bitmaps. Additionaly, I have no idea how to fill out bmPlanes and bmPixels.
    bmPlanes
    Specifies the count of color planes.
    bmBitsPixel
    Specifies the number of bits required to indicate the color of a pixel.
    I have no idea where I'd get that information.

    So I wandered off to CreateCompatableBitmap...
    For the CreateCompatableBitmap function, it takes an hdc
    Code:
    HBITMAP CreateCompatibleBitmap(
      HDC hdc,        // handle to DC
      int nWidth,     // width of bitmap, in pixels
      int nHeight     // height of bitmap, in pixels
    );
    Example to avoid creating a monochrome image:
    Code:
    HDC memDC = CreateCompatibleDC ( hDC );
    HBITMAP memBM = CreateCompatibleBitmap ( hDC, nWidth, nHeight );
    SelectObject ( memDC, memBM );
    But I'm lost as to what device I'd give the hdc that would also give it the bitmap information (Msdn being vague again -,-). It looks to me like resources might be quicker o_O.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I was wondering if theres some way to copy the bitmap contents to a constant string, then load it?
    Check out this code by JaWib.
    Code:
    #include <windows.h>
    #include <iostream>
    
    class Bitmap
    {
    public:
      Bitmap(const char* file)
      {
        m_hImage = (HBITMAP)LoadImage(GetModuleHandle(0),file,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
      }
      
      bool operator!() { return m_hImage==0?true:false; }
    
      void Draw()
      {
        HDC hdc = GetDC(GetConsoleWindow());
        BITMAPINFO bmi = {0};
        bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        if (!GetDIBits(hdc,m_hImage,0,0,0,&bmi,DIB_RGB_COLORS))
          std::cerr<<"Failed to get bitmap info";
       
        int width = bmi.bmiHeader.biWidth;
        int height = bmi.bmiHeader.biHeight;
        
        char* bits = new char[width*height*bmi.bmiHeader.biBitCount+sizeof(BITMAPINFOHEADER)];
        memcpy(bits, &bmi.bmiHeader, sizeof(BITMAPINFOHEADER));
        
        if(!GetDIBits(hdc, m_hImage, 0, height, bits+bmi.bmiHeader.biSize, (LPBITMAPINFO)bits, DIB_RGB_COLORS))
          std::cerr<<"Failed to get bits: "<<GetLastError();
        
        if (!SetDIBitsToDevice(hdc,0,0,width,height,0,0,0,height,bits+bmi.bmiHeader.biSize,(LPBITMAPINFO)bits,DIB_RGB_COLORS))
          std::cerr<<"Failed to draw"<<GetLastError();
        ReleaseDC(GetConsoleWindow(),hdc);
      }
    private:
      HBITMAP m_hImage;
    };
    
    
    std::ostream& operator<<(std::ostream& os, Bitmap& image)
    {    
      image.Draw();
      return os;
    }
    
    
    
    int main()
    {
      Bitmap image("image.bmp");
      if(!image)
      {
        std::cerr<<"Failed to load image";
        return 1;
      }
      std::cout<<image;
      
    }
    It was posted under this thread: Display Images?
    Last edited by swoopy; 09-02-2006 at 02:26 AM.

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Wow, thats going to take a minute to figure out haha. In the meantime, I think I'll stick with resources until I can understand what the hell thats doing lol. Thanks tonto & swoopy.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Wow, thats going to take a minute to figure out haha.
    Same here, I haven't looked at it in detail, but remember thinking when I originally browsed the thread that this is some nifty code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21