loading image for a height map

I simply want to load a grayscale image (bitmap targa whever is easy) and to save the colour values and and array of ints. How? I tried GetPixel and LoadImage but one uses HBITMAP and the other HDC, i am really confused, any help would be greatly appreciated.

Comments

  • I hope you get a reply, because I would really like to know how to do this as well!! Good question.

    -John.


    : I simply want to load a grayscale image (bitmap targa whever is easy) and to save the colour values and and array of ints. How? I tried GetPixel and LoadImage but one uses HBITMAP and the other HDC, i am really confused, any help would be greatly appreciated.
    :

  • [b][red]This message was edited by kreitler at 2003-7-19 9:12:36[/red][/b][hr]
    Hey guys,
    You're both very close to having the answer. You're just missing the middle step. You can use this function:

    [code]
    HDC memDC = CreateCompatibleDC(NULL);
    [/code]

    to create a device context that matches the current video mode, then select the HBITMAP into it using SelectObject:

    [code]
    HBITMAP oldBMP = SelectObject(memDC, newBitmapHandle);
    [/code]

    (where newBitmapHandle is the HBITMAP you loaded in earlier).

    Once you have the bitmap loaded into a device context, you should be able to operate on it with GetPixel, etc.

    Once you're done reading the greyscale info, don't forget to clean up:

    [code]
    SelectObject(memDC, oldBMP);
    DeleteDC(memDC);
    [/code]


    Hope that helps.

    Kreitler



    : I hope you get a reply, because I would really like to know how to do this as well!! Good question.
    :
    : -John.
    :
    :
    : : I simply want to load a grayscale image (bitmap targa whever is easy) and to save the colour values and and array of ints. How? I tried GetPixel and LoadImage but one uses HBITMAP and the other HDC, i am really confused, any help would be greatly appreciated.
    : :
    :
    :



  • thanks, but i am still having troubles, he is the code if you could tell me where i am going wrong that would be great.

    int main(int argc, char **argv)
    {
    HBITMAP bground;

    bground = LoadBitmapFile("ass.bmp");

    HDC memDC = CreateCompatibleDC(NULL);


    HBITMAP newground = SelectObject(memDC, bground);

    COLORREF bob;

    bob = GetPixel(bground,200,100);


    SelectObject(memDC, newground);
    DeleteDC(memDC);

    return 0;
    }

    HBITMAP LoadBitmapFile(char *filename)
    { //Simple one-liner
    return (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    }

    i get this error

    cannot convert from 'void *' to 'struct HBITMAP__ *'

    thanks phil
  • Hey PunkPhil,
    Here's your problem:

    [code]
    [red]
    HBITMAP newground = SelectObject(memDC, bground);
    [/red]
    [/code]

    SelectObject can return many different kinds of objects: handles to bitmaps, brushes, fonts, etc. Therefore, its return type is (void *). You need to cast this result before you can assign it to an HBITMAP:

    [code]
    [blue]
    HBITMAP newground = (HBITMAP)SelectObject(memDC, bgound);
    [/blue]
    [/code]

    Give that a whirl.
    There may be other problems with the program--I'm at work, so I didn't give it a thorough look. If so--and you get stumped--let me know.

    Good luck,

    Kreitler


    : thanks, but i am still having troubles, he is the code if you could tell me where i am going wrong that would be great.
    :
    : int main(int argc, char **argv)
    : {
    : HBITMAP bground;
    :
    : bground = LoadBitmapFile("ass.bmp");
    :
    : HDC memDC = CreateCompatibleDC(NULL);
    :
    :
    : HBITMAP newground = SelectObject(memDC, bground);
    :
    : COLORREF bob;
    :
    : bob = GetPixel(bground,200,100);
    :
    :
    : SelectObject(memDC, newground);
    : DeleteDC(memDC);
    :
    : return 0;
    : }
    :
    : HBITMAP LoadBitmapFile(char *filename)
    : { //Simple one-liner
    : return (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    : }
    :
    : i get this error
    :
    : cannot convert from 'void *' to 'struct HBITMAP__ *'
    :
    : thanks phil
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories