#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    28
    Rep Power
    0

    Reading Image File(JPEG)


    Hi

    How can I Open an Image files in C, like jpeg, bmp, gif

    Thx
    Sumesh
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Russia
    Posts
    2
    Rep Power
    0
    Jpeg-Gif-Bmp sample for LCC, BC, VC

    // LCC - link with uuid.lib

    #define LccVersion // only for LCC Compiler

    #include <windows.h>
    #include <olectl.h>

    #define MAX_LOADSTRING 100
    #define HIMETRIC_INCH 2540
    #define MAP_LOGHIM_TO_PIX(x,ppli) ( ((ppli)*(x) + HIMETRIC_INCH/2) / HIMETRIC_INCH )

    #define FileName "test.jpg"

    LPPICTURE gpPicture;
    HANDLE hBitmap ;

    //---------------------------------------------------------------------------

    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    void LoadPictureFile(LPCTSTR szFile);

    //---------------------------------------------------------------------------

    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    {
    static char szAppName[] = "JpegTest" ;
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wndclass;

    int WinSize = 500;
    int Left = 200;
    int Top = 100;

    wndclass.cbSize = sizeof (wndclass) ;
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc ;
    wndclass.cbClsExtra = 0 ;
    wndclass.cbWndExtra = 0 ;
    wndclass.hInstance = hInstance ;
    wndclass.hIcon = NULL ;
    wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW) ;
    wndclass.lpszMenuName = NULL ;
    wndclass.lpszClassName = szAppName ;
    wndclass.hIconSm = NULL ;

    RegisterClassEx (&wndclass) ;

    hwnd = CreateWindow (
    szAppName, // pointer to registered class name
    "Transparent Clock", // pointer to window name
    WS_OVERLAPPEDWINDOW, // window style
    Left, // horizontal position
    Top , // vertical position
    WinSize, // window width
    WinSize, // window height
    NULL, // handle to parent or owner window
    NULL, // handle to menu or child-window identifier
    hInstance, // handle to application instance
    NULL // pointer to window-creation data
    ) ;

    ShowWindow (hwnd, iCmdShow);
    UpdateWindow (hwnd);

    while (GetMessage (&msg, NULL, 0, 0))
    {
    TranslateMessage (&msg) ;
    DispatchMessage (&msg) ;
    }
    return msg.wParam ;
    }

    //---------------------------------------------------------------------------

    LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
    {
    HDC hdc, hCompatibleDC ;
    PAINTSTRUCT ps ;
    RECT rect;

    HANDLE hOldBitmap ;
    BITMAP Bitmap;

    switch (iMsg)
    {
    case WM_CREATE :
    LoadPictureFile(FileName);
    return 0 ;

    case WM_PAINT:
    hdc = BeginPaint(hwnd, &ps);
    if (gpPicture)
    {
    // get width and height of picture
    long hmWidth;
    long hmHeight;
    gpPicture->get_Width(&hmWidth);
    gpPicture->get_Height(&hmHeight);

    // convert himetric to pixels
    int nWidth = MulDiv(hmWidth, GetDeviceCaps(hdc, LOGPIXELSX), HIMETRIC_INCH);
    int nHeight = MulDiv(hmHeight, GetDeviceCaps(hdc, LOGPIXELSY), HIMETRIC_INCH);

    RECT rc;
    GetClientRect(hwnd, &rc);

    // display picture using IPicture::Render
    gpPicture->Render(hdc, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, &rc);
    }
    EndPaint(hwnd, &ps);
    return 0 ;

    case WM_DESTROY :
    PostQuitMessage (0) ;
    return 0 ;
    }
    return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
    }

    //------------------------------------------------------------------------------------------------//

    // This function loads a file
    void LoadPictureFile(LPCTSTR szFile)
    {
    // open file
    HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);

    // get file size
    DWORD dwFileSize = GetFileSize(hFile, NULL);

    LPVOID pvData = NULL;
    // alloc memory based on file size
    HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);

    pvData = GlobalLock(hGlobal);

    DWORD dwBytesRead = 0;
    // read file and store in global memory
    BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
    GlobalUnlock(hGlobal);
    CloseHandle(hFile);

    LPSTREAM pstm = NULL;
    // create IStream* from global memory
    HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);

    // Create IPicture from image file
    if (gpPicture)
    gpPicture->Release();

    #ifdef LccVersion
    hr = OleLoadPicture(pstm, dwFileSize, FALSE, &IID_IPicture, (LPVOID *)&gpPicture);
    #else
    hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
    #endif

    pstm->Release();

    // InvalidateRect(ghWnd, NULL, TRUE);
    }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    1
    Rep Power
    0

    Compile Error


    This code gives a 'Render not a member of IPICTURE' error 2039.

IMN logo majestic logo threadwatch logo seochat tools logo