Thread: Image opening with Visual C++

  1. #1
    george7378
    Guest

    Image opening with Visual C++

    Hi everyone,

    I am able to create GUI applications, and can create a window, but I don't know how to work with images. What commands can I use to display a picture in a window? Do you have any examples?

    Thanks.

  2. #2
    george7378
    Guest
    Maybe someone could giv me an example of how a Bitmap resource looks in the .rc file?

    Thanks.

  3. #3

  4. #4
    george7378
    Guest
    Thanks. This is the content of my .rc file:

    Code:
    #include "resource.h"
    #include "windows.h"       
    
    IDB_BITMAP1                BITMAP  DISCARDABLE     "Image.bmp"
    ...However, I get the following compilation error:

    Code:
    fatal error RC1004: unexpected end of file found

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Forgot to include stdafx.h?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    george7378
    Guest
    Thanks - I added it, but now I get:
    Code:
    fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    What compiler are you using? In VC++ you should write
    Code:
    #include <windows.h>
    You should include stdafx.h only if you are using precompiled headers and it include file is stdafx.h.

    Please post another code and header file of your program if possible.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is related to the source and not to precompiled headers or stdafx.h.
    So, posting your resource.h and the resource file might not be a bad idea.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    george7378
    Guest
    Thanks,

    I started with an empty project in the 'General' tab in VC++ 2008 Express. Here is the code:

    Source:

    Code:
    #include <windows.h>
    #include <stdafx.h>
    #include "Resource.h"
    
    HINSTANCE hInst;
    LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX  WndCls;
        static char szAppName[] = "BitmapIntro";
        MSG         Msg;
    
    	hInst       = hInstance;
        WndCls.cbSize        = sizeof(WndCls);
        WndCls.style         = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
        WndCls.lpfnWndProc   = WindProcedure;
        WndCls.cbClsExtra    = 0;
        WndCls.cbWndExtra    = 0;
        WndCls.hInstance     = hInst;
        WndCls.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        WndCls.hCursor       = LoadCursor(NULL, IDC_ARROW);
        WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        WndCls.lpszMenuName  = NULL;
        WndCls.lpszClassName = szAppName;
        WndCls.hIconSm       = LoadIcon(hInstance, IDI_APPLICATION);
        RegisterClassEx(&WndCls);
    
        CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                              szAppName,
                              "Bitmap",
                              WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              NULL,
                              NULL,
                              hInstance,
                              NULL);
    
        while( GetMessage(&Msg, NULL, 0, 0) )
        {
            TranslateMessage(&Msg);
            DispatchMessage( &Msg);
        }
    
        return static_cast<int>(Msg.wParam);
    }
    
    LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg,
    			   WPARAM wParam, LPARAM lParam)
    {
        HDC hDC, MemDCExercising;
        PAINTSTRUCT Ps;
        HBITMAP bmpExercising;
    
        switch(Msg)
        {
    	case WM_DESTROY:
    	    PostQuitMessage(WM_QUIT);
    	    break;
    	case WM_PAINT:
    	    hDC = BeginPaint(hWnd, &Ps);
    
    	    bmpExercising = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
    	    MemDCExercising = CreateCompatibleDC(hDC);
                 SelectObject(MemDCExercising, bmpExercising);
    
    	    BitBlt(hDC, 10, 10, 450, 400, MemDCExercising, 0, 0, SRCCOPY);
    
    	    DeleteDC(MemDCExercising);
    	    DeleteObject(bmpExercising);
    	    EndPaint(hWnd, &Ps);
    	    break;
    	default:
    	    return DefWindowProc(hWnd, Msg, wParam, lParam);
        }
        return 0;
    }
    Resource (I have no idea how to write a Bitmap resource):
    Code:
    #include "resource.h"
    #include "windows.h"       
    
    IDB_BITMAP1     BITMAP  DISCARDABLE     "C:\Documents and Settings\XP\My Documents\My 
    
    Pictures\Image.bmp"
    Header:

    Code:
    #include "windows.h"
    
    #define IDB_BITMAP1               1000
    Thanks.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    put
    Code:
    #include <stdafx.h>
    in .rc file
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    george7378
    Guest
    I've managed it another way - I think it may have been my resource editor program that wasn't set up properly, as I re-made my .rc file, and it gave me this:

    Code:
    #include <windows.h>
    #include <commctrl.h>
    #include <richedit.h>
    #include "resource.h"
    
    
    LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
    IDB_BITMAP1        BITMAP         "bitmap1.bmp"

    http://i302.photobucket.com/albums/n...ageprogram.png

  12. #12
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I can't follow link.

    Provide that here please.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  13. #13
    george7378
    Guest
    I don't know how to do that - how do I embed images on this forum? Here is another hyperlink:

    Link

    The next thing I am interested in doing is downloading an image from the net and displaying it in that frame. For example, if I want to download this image every time the program starts:

    http://sohowww.nascom.nasa.gov/data/...024/latest.jpg

    Is this hard t do?

    Thanks.
    Last edited by george7378; 06-15-2010 at 06:03 AM.

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I just don't.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The next thing I am interested in doing is downloading an image from the net and displaying it in that frame. For example, if I want to download this image every time the program starts:
    So you can't load images yet and you already want to do another task related to images? Google is your friend. Do a little research and come back with a specific directed question about the problems you are having and we can offer more assistance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. What's wrong with this code??
    By Xanth in forum C Programming
    Replies: 11
    Last Post: 12-23-2004, 02:41 PM
  4. Do I need Visual C++ for image display
    By algi in forum C++ Programming
    Replies: 7
    Last Post: 11-20-2004, 04:08 PM
  5. MS Visual C++ 6 wont include vector.h in a header file
    By bardsley99 in forum C++ Programming
    Replies: 9
    Last Post: 11-06-2003, 12:05 PM