CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2009
    Posts
    2

    Problem Creating a Color BITMAP

    Hi,

    I've created a dummy char buffer and am trying to write the values to bitmap to display.

    My code fails on the SetDIBits functions, it returns a zero.

    Here's my code:

    // Create Dummy Image Array

    unsigned char * imageArray1;
    int size=48 * 48 * 3 ;
    imageArray1=new unsigned char[size];
    ::memset(imageArray1,192,size);


    //void pointer to the Image Array

    void* lpImageBits=imageArray1;


    //Create Compatible Bitmap

    HDC hDC,hMemoryDC;
    hDC=::GetDC(this.m_hWnd);
    HBITMAP hBitmap=CreateCompatibleBitmap(hDC,48,48);
    if (hBitmap==NULL) MessageBox("Bad hBitmap","",0);


    //Get BITMAP that corespondes the the image array.

    BITMAP bm;
    GetObject((HGDIOBJ)hBitmap, sizeof(BITMAP), &bm);
    bm.bmBits=imageArray1;

    // This function Fails and returns 0 which means it didn't write any data

    int rtLines=SetDIBits(hDC,hBitmap,0,47,imageArray1,(BITMAPINFO*)&bm,DIB_RGB_COLORS);

    // Create Compatible DC and Select hBitmap in it.

    hMemoryDC=::CreateCompatibleDC(hDC);
    if (hMemoryDC==NULL) MessageBox("Error Creating Compatible DC","",0);

    void* returnBitmap=SelectObject(hMemoryDC,hBitmap);
    if (returnBitmap==NULL) MessageBox("Error SelectObject","",0);

    //BitBlt to Screen. Only get black rectangle. No data written.

    error_Level=::BitBlt(hDC,150,20,48, 48, hMemoryDC, 0, 0, SRCCOPY);

    Any ideas?

    Much appreciated.

    Regards
    valinc

  2. #2
    Join Date
    Aug 2009
    Location
    The Netherlands
    Posts
    7

    Re: Problem Creating a Color BITMAP

    Why don't you just create the bitmapheader yourself and write it to disk with the WriteFile() API.

  3. #3
    Join Date
    Sep 2009
    Posts
    57

    Re: Problem Creating a Color BITMAP

    It would be better to make a struct that will have your .bmp variables
    Check this link: http://www.computing.net/answers/pro...lly/14840.html

  4. #4
    Join Date
    Nov 2007
    Posts
    613

    Re: Problem Creating a Color BITMAP

    You're mixing functions for device dependent bitmaps (CreateCompatibleBitmap) with functions for device independent bitmaps (SetDIBits).

    In order to make SetDIBits to work, you need to create the bitmap using CreateDIBSection.

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