CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Guest

    Newbie: How do I create a DIB from an array ?

    Greetings

    I have a 256x256 array containing RGB values.Each value represents one out of 256 possible shades of gray.
    The problem is that i don't know how to use this array to create a DIB.
    I read about CreateDIBSection() function in help but being a newbie I cannot understand how to use it.

    Could anyone help me?

    Thanks in advance

    George


  2. #2
    Join Date
    May 1999
    Posts
    8

    Re: Newbie: How do I create a DIB from an array ?

    It's very simple:



    struct
    {
    BITMAPINFOHEADER bmiHeader;
    RGBQUAD bmiColors[256];
    }BMInfo;


    ZeroMemory(&BMInfo, sizeof(BITMAPINFOHEADER));
    BMInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);


    BMInfo.bmiHeader.biWidth = 256; //X size, must be byte aligned
    BMInfo.bmiHeader.biHeight = 256;//Y size


    BMInfo.bmiHeader.biPlanes = 1;//always 1
    BMInfo.bmiHeader.biCompression = BI_RGB;


    BMInfo.bmiHeader.biBitCount = 24;//Bits per Pixel(in our sample RGB)
    BMInfo.bmiHeader.biClrUsed = 0;//ignored for True Color


    BYTE* pVideoBuffer = 0;//prepare pointer for DIB
    HBITMAP hBitmap = ::CreateDIBSection(NULL, (BITMAPINFO*)BMInfo,
    DIB_RGB_COLORS, &pVideoBuffer, NULL, 0);

    if(!hBitmap)
    {//error create DIB Section
    }

    //now you have a valid pointer to buffer, copy you picture here
    //or fill it directly.
    //for example copy:
    memcpy(pVideoBuffer, pYouSourceBuffer, SizeX * SizeY *
    BMInfo.bmiHeader.biBitCount/8);

    //then you may select, bitmap in DC.
    MemDC.CreateCompatibleDC(NULL);

    HBITMAP hOldBitmap = ::SelectObject(MemDC.GetSafeHdc(), hBitmap);
    //then draw on it DC, make BitBlt, etc.
    ........................................
    ........................................
    //In this code removed error check, be carefull !




    Andrey.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured