bitmap

This is a discussion on bitmap within the Windows Programming forums, part of the Platform Specific Boards category; Ok, I want to open a bitmap, CString fileName; fileName = "somefile.bmp"; CBitmap bmCrit; bmCrit.LoadBitmap(fileName); when i do above i ...

  1. #1
    swordfish
    Guest

    Unhappy bitmap

    Ok, I want to open a bitmap,

    CString fileName;
    fileName = "somefile.bmp";

    CBitmap bmCrit;
    bmCrit.LoadBitmap(fileName);

    when i do above i get an illegal operation when program runs. please tell me where i am going wrong? and i set the file name from the file dialogue(even if i don't i get the same error).

  2. #2
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    LoadBitmap does not expect a filename, but a resource name (the function's name may be confusing, indeed).


    If you're using MFC, then first load a bitmap (can be done in the ResourceView), and use it's ID in the LoadBitmap.

    {
    CBitmap bmp; // Creating CBitmap Object bmp
    bmp.LoadBitmap(ID_OF_THE_RESOURCE_BMP);
    ...
    }

    Afterwards you can use methods like SetBitmap, SetItemImage, etc. to attach the bitmap to a given control, element (button, treeview, and so on).

    Hint:buy Windows programming related books, and forget the old Pascal approach .

  3. #3
    swordfish
    Guest
    well the problem is i want the user to be able to select the bitmap they want to insert? how would i go about doing that.

  4. #4
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Try this out:

    CBitmap* aCBmp = NULL;
    if(!aCBmp->LoadBitmap("Bitmaps\\bitmap.bmp"))
    {
    MessageBox("Cannot load bitmap from file","Load error!", MB_OK);
    }

  5. #5
    Unregistered
    Guest
    Ok , here is how i am doing it

    CDC dcMem;

    CBitmap* aCBmp = NULL;

    if(!aCBmp->LoadBitmap("setup.bmp"))
    {

    //**************// an illagal operation is reported by
    //**************// windows and program terminates before
    //**************// message box comes
    MessageBox(NULL, "Cannot load bitmap from file","Load error!", MB_OK);
    }



    MessageBox(NULL, "1", NULL, MB_OK);




    aCBmp->GetObject(sizeof(BITMAP), aCBmp);

    dcMem.CreateCompatibleDC(pDC);
    VERIFY(dcMem.SelectObject(aCBmp));



    why is it generating illegal operation while loading . (setup.bmp is not a defined resource, and the file does exist in the running directory)

  6. #6
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Give it a try without pointers.

    CBitmap aCBmp;

    if(!aCBmp.LoadBitmap("winnt.bmp"))
    {

    MessageBox("Cannot load bitmap from file","Load error!", MB_OK);
    }

  7. #7
    Unregistered
    Guest
    The message box

    MessageBox("Cannot load bitmap from file","Load error!", MB_OK);


    do appear this time, indeed it always appear. so i am still unable to load a file. i have tried to give different file name but still no vain. what should i do now.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi

    try LoadImage() API with
    LR_CREATEDIBSECTION - if you like it as DIB image
    and LR_LOADFROMFILE to load it from the file instead of resources

    damyan

  9. #9
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    "LoadBitmap does not expect a filename, but a resource name "
    Right after I said this, made myself the mistake !
    Ok, I was wrong .
    Now, the *working* code look like this (there are some checkings, too)
    /* --------------------------------------------------
    loading the bitmap from file and attaching to a CBitmap
    --------------------------------------------------*/
    CBitmap aCBmp;
    HBITMAP hBmp;
    BITMAP mBitmap;

    FILE *File=NULL; // File Handle
    File = fopen("your_bmp_name_here","r");

    // file exists?
    if ( File )
    {
    fclose(File);// Close The Handle
    MessageBox("File open ok","Open success", MB_OK);

    // Load the Bitmap and return a pointer
    hBmp = (HBITMAP)::LoadImage( 0, "your_bmp_name_here", IMAGE_BITMAP, 0, 0,
    LR_DEFAULTCOLOR |
    LR_LOADFROMFILE |// load flags);
    LR_CREATEDIBSECTION
    );
    }

    if ( !hBmp )
    {
    MessageBox("Bitmap load failed","Load error!", MB_OK);
    }

    if ( !aCBmp.Attach(hBmp) )
    {
    MessageBox("Attach failed","Error!", MB_OK);
    }

    if ( !aCBmp.GetBitmap( &mBitmap ) )
    {
    MessageBox("GetBitmap failed","Error!", MB_OK);
    }
    }

    /* --------------------------------------------------
    Put these in the OnPaint event
    to display your bmp
    --------------------------------------------------*/
    void yourClass::OnPaint
    {
    // do not modify
    if (IsIconic())
    {
    //do not modify
    }
    else
    // your code comes here!
    // which loads CBitmap to a memDC, then copies it to the PaintDC

    CDC memDC;
    CPaintDC dcPaint(this);

    if( memDC.CreateCompatibleDC(&dcPaint) )
    {
    memDC.SelectObject( &aCBmp );

    dcPaint.BitBlt( 0, 0,
    iYourWidth,
    iYourHeight,
    &memDC,
    0,
    0,
    SRCCOPY);
    }
    CDialog::OnPaint();
    }
    }

    You can make a LoadBmp method, this would be more elegant, passing the filename as parameter.

    And don't forget to call the DeleteObject function (e.g. in your destructor) to delete each bitmap handle returned by the LoadBitmap function.

    Have fun! (and register yourself here, it's a great site)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 08:43 AM
  2. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  3. Loading a Bitmap resource for OpenGL Texture[x]
    By the dead tree in forum Game Programming
    Replies: 4
    Last Post: 08-26-2004, 01:12 PM
  4. bitmap not going onto screen
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 02-22-2003, 09:07 AM
  5. texture is all white in opengl!
    By Crossbow in forum Game Programming
    Replies: 7
    Last Post: 03-31-2002, 10:54 AM

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