Thread: Saving a bitmap.

  1. #1
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    Saving a bitmap.

    How do I save a bitmap if all I know is the pixel data of an HDC
    that I want to save it from?

    What I want to do is 1) read all the pixel data from the 1st HDC
    (which has the original bitmap loaded into it). Then 2) blit those
    pixels into another HDC with modifications. Lastly 3) save the
    image that was blitted into the 2nd HDC as a bitmap.

    I know how to do steps 1 and 2 with GetPixel and SetPixel, I just
    don't know how to save the contents of the 2nd HDC into
    a bitmap file.

    I know the structure of a bitmap file and I know the file will be a
    16-bit bitmap.

    All I can think of would be if I have to define my own
    BITMAPFILEHEADER and BITMAPINFOHEADER, populate an array
    of COLORREFs (that would hold pixel data). Then use CreateFile
    and WriteFile to make the bitmap. I was just wondering (hoping)
    if there was a function that made it easier/less combersome than
    that.

    Thanks in advance for any help.
    Last edited by jdinger; 04-29-2002 at 05:37 PM.

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Just wanted to point out, no big rush on this. I've got a temp fix
    using VB and the SavePicture function, but I'd really like to do it in
    C++.

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    well i would recommend writing a loader/saver for yourself! i did, and it did me good

    a really good doc on the full BMP format is here it even explains RLE compression ect...
    http://www.wotsit.org/download.asp?f=bmpfrmat
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I only use jpg's so I'm not sure if this will work.

    Could you use SelectObject() to 'push' out the bitmap from the HDC?
    CreateCompatibleBitmap() for the new bmp and use BitBlt() if you need a copy.
    GetObject() to find its properties (fill a BITMAP struct from the HBITMAP provided)
    Use GetDIBits() to create the BITMAPINFO struct that can be saved.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Could you use SelectObject() to 'push' out the bitmap from the HDC?
    CreateCompatibleBitmap() for the new bmp and use BitBlt() if you need a copy.
    GetObject() to find its properties (fill a BITMAP struct from the HBITMAP provided)
    Use GetDIBits() to create the BITMAPINFO struct that can be saved.
    Thanks, novacain. I tried it your way and (including error checking)
    I got it down to about 50 lines of code.

    Back in my VB days ( the dark ages... ) I used a function called
    SavePicture which took 2 params: the image to copy from and the
    filename to save it as.

    What I did now was write a my own C++ function that takes
    2 params: the hdc of that holds the image and the filename to
    save it to.

    So I got the simple 1 line function to save my bitmaps that I was
    accustomed to and I learned a ton by writing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmap saving problem
    By geek@02 in forum Windows Programming
    Replies: 3
    Last Post: 10-01-2007, 08:44 PM
  2. Saving the static control's HDC into bitmap
    By Joelito in forum Windows Programming
    Replies: 2
    Last Post: 09-13-2007, 09:22 PM
  3. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 09:43 AM
  4. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  5. bitmap not going onto screen
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 02-22-2003, 10:07 AM