MFC program, load and display image

Posted on 2006-05-19
Last Modified: 2013-11-20
My orignial program went through some conflict so I need one part of it.
I need a code for a MFC program that has two windows.

The main window has a button that loads a bitmap file and has an edit box to display where the file has been loaded, and the other window has nothing except for displaying the bitmap image.
Question by:killaz4life
    LVL 22

    Expert Comment

    You may take help of following samples ...refer with src

    Also have a look at various examples at codeguru bitmap section :


    Author Comment

    Mahest I think you remember my other question about converting a raw image to bmp. This apparently became my problem since its not working. The 1st website, i already know, 2nd website, is very hard to follow understand the 3rd website is irrelevant to my question. ALL i mainly need is a function that displays the image on the 2nd window and if any other function has any relationship what other code should i add!

    Author Comment

    Sorry there is a line missing, My problem was that i cannot convert an image since i cannot display an image to begin with! Thats why i need the code for displaying an image
    LVL 22

    Accepted Solution

    >> i mainly need is a function that displays the image on the 2nd window

    Simple function to display BITMAP will be something like this :

    pass hDC of your window, bitmap file path strBitmap, position X,Y

    BOOL DisplayBitmap(HDC hDC, CString strBitmap,int X, int Y)
     * Steps to displaying a bitmap:
     * 1. Create a compatible display context.
     * 2. Select bitmap into compatible display context.
     * 3. Do a bit-block-transfer (BitBlt) command.
     * 4. Clean-up afterwards.
          HDC     hCompatDC;
          HBITMAP hOldBitmap;
          BITMAP  BM;
          BOOL    result;

                    HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(), strBitmap, IMAGE_BITMAP, 0, 0,      

          // Create compatible display context
          hCompatDC = CreateCompatibleDC(hDC);

          // Select bitmap into compatible display context
          hOldBitmap = SelectBitmap(hCompatDC, hBitmap);

          // Get dimensions of bitmap
          GetObject(hBitmap, sizeof(BM), &BM);

          // Blast those bits to the screen
          result = BitBlt(hDC, X, Y, BM.bmWidth, BM.bmHeight, hCompatDC, 0, 0, SRCCOPY);

          // De-select the bitmap
          SelectBitmap(hCompatDC, hOldBitmap);

          // Clean up after we are done

          return result;

    >>My problem was that i cannot convert an image since i cannot display an image to begin with !

    For conversion purpose you do not need to display image all you need is bitmap data buffer (DIB) to process on...

    LVL 11

    Expert Comment

    Seems like you need an MDI application, Or SDI with a splitter window and you can use GDI+ too. That looks cool on your application.

    Please follow this link you might get what you want ...!

    Best Regards,

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
    Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now