win32 API open a jpg in a picture control c++

win32 API open a jpg in a picture control c++

The OleLoadPicturePath API function can load a JPEG file.

Then its just a matter of accessing the bits.

Theres also the Windows Imaging Component API, but I havent used that.

I suspect that it also works, though, and it may be simpler than dealing with the OLE stuff, but here I exemplify OleLoadPicturePath.

Before trying to adapt the code below, you should:

  • Make sure that the type of the picture control resource is set to BITMAP (essentially, at the .rc text level, that it has the SS_BITMAP style).

  • Change the ID to something unique, instead of IDC_STATIC.

enter

#include <header_wrapper/olectl_h.h>        // IPicture
#include <header_wrapper/windows_h.h>
#include resource.h       // IDD_DEMO_DIALOG, IDC_PICTURE

#include <progrock/cppx/throwx.h>           // hopefully, throwX, std::exception
#include <progrock/cppx/size.h>             // size
#include <progrock/winapi/path.h>           // *
#include <progrock/winapi/ComPointer.h>     // ComPointer
using namespace progrock;

#include <assert.h>         // assert
#include <iostream>
#include <stdlib.h>         // EXIT_FAILURE, EXIT_SUCCESS
using namespace std;

using cppx::hopefully;
using cppx::size;
using cppx::throwX;

using winapi::String;

struct IsHrSuccess
{
    friend bool operator>>( HRESULT const hr, IsHrSuccess const& )
    {
        return SUCCEEDED( hr );
    }
};

IsHrSuccess const   isHrSuccess = IsHrSuccess();

short kindOf( IPicture const& pic )
{
    short kind  = 0;
    const_cast< IPicture& >( pic ).get_Type( &kind )
        >> isHrSuccess || throwX( kindOf: IPicture::get_Type failed );
    return kind;
}

bool isBitmap( IPicture const& pic )
{
    return (kindOf( pic ) == PICTYPE_BITMAP);
}

OLE_HANDLE handleOf( IPicture const& pic )
{
    OLE_HANDLE  result  = 0;
    const_cast< IPicture& >( pic ).get_Handle( &result )
        >> isHrSuccess || throwX( handleOf: IPicture::get_Handle failed );
    return result;
}

HBITMAP bmpHandle( IPicture const& pic )
{
    assert( isBitmap( pic ) );
    return reinterpret_cast< HBITMAP >( handleOf( pic ) );
}

namespace g {
    winapi::ComPointer<IPicture>  pPicture;
}  // namespace g

INT_PTR CALLBACK demoDialogProc(
    HWND const      window,
    UINT const      messageId,
    WPARAM const    wParam,
    LPARAM const    lParam
    )
{
    switch( messageId )
    {
    case WM_INITDIALOG:
        ::SendDlgItemMessage(
            window, IDC_PICTURE, STM_SETIMAGE,
            IMAGE_BITMAP,
            reinterpret_cast< LPARAM >( bmpHandle( *g::pPicture ) )
            );
        break;
    case WM_CLOSE:
        ::EndDialog( window, IDCANCEL );
        break;
    case WM_COMMAND:
        ::EndDialog( window, wParam );
        break;
    }
    return 0;
}

struct Com
{
    Com() { ::CoInitialize( 0 ) >> isHrSuccess || throwX( ::CoInitialize failed ); }
    ~Com() { ::CoUninitialize(); }
};

void cppMain()
{
    Com usingCom;
    String const    picFileName     = Limage.jpg;
    String const    picFilePath     = winapi::path::combine( winapi::exeFolder(), picFileName );

    ::OleLoadPicturePath(
        const_cast< wchar_t* >( picFilePath.c_str() ),
        nullptr, 0, 0,
        IID_IPicture,
        g::pPicture.asOutArgument()
        )
        >> isHrSuccess || throwX( OleLoadPicturePath failed );
    assert( isBitmap( *g::pPicture ) );

    ::DialogBox( ::GetModuleHandle( 0 ), MAKEINTRESOURCE( IDD_DEMO_DIALOG ), 0, demoDialogProc );
}

int main()
{
    try
    {
        cppMain();
        return EXIT_SUCCESS;
    }
    catch( exception const& x )
    {
        wcout << ! << x.what() << endl;
    }
    return EXIT_FAILURE;
}

win32 API open a jpg in a picture control c++

Leave a Reply

Your email address will not be published.