1.準備

 「GDI+を使った画像処理」ではGDI+を使ったjpeg , gif などの画像ファイルの処理方法について説明します。
 なおここで扱うのは.NETではなくネイティブAPIの方のGDI+です。

 プログラムでGDI+ を使うには次の準備が必要です。
1.インクルードファイルgdiplus.h をインクルード
2.名前空間Gdiplus を宣言
3.プログラムの最初で関数GdiplusStartup を呼び出す。
4.プログラムの最後で関数GdiplusShutdown を呼び出してGDI+を終了させる。
5.プロジェクトにGdiplus.lib を参加させる

 これらをを実際のプログラムで説明すると以下の例のようになります。
 なお下のプログラムでは、ウインドウ手続き関数 WndProc の中身がないので動作しません。


#include <windows.h>
#include <gdiplus.h> <---1.gdiplus.h をインクルード

/* 名前空間宣言 */
using namespace Gdiplus ; //introduces namespace std <---2.名前空間Gdiplus を宣言

/* グローバル変数 */
GdiplusStartupInput gdip_SI; <---- 関数GdiplusStartup 、GdiplusShutdownで使用
ULONG_PTR     gdip_Token;<---関数GdiplusStartup 、GdiplusShutdownで使用

/* +++++ 関数宣言 +++++ */
int WINAPI WinMain( HINSTANCE , HINSTANCE , LPSTR ,int ) ;
BOOL Register_WndClass( HINSTANCE , TCHAR* ) ; //ウインドウクラス登録用関数
HWND Set_Create_Window( HINSTANCE ,TCHAR* ,int ) ;

LRESULT CALLBACK WndProc( HWND , UINT ,WPARAM , LPARAM );

/* +++++ 関数定義 +++++ */
int WINAPI WinMain( HINSTANCE hInst, /*Win32 entry-point routine */
             HINSTANCE hPreInst,
             LPSTR lpszCmdLine, //コマンドライン(ファイルから起動など)
             int nCmdShow )

{
   HWND hWnd ; //ウインドウへのハンドル
   MSG lpMsg; //メッセージ(イヴェントみたいなもの)
   TCHAR szWinName[] = TEXT("testprog"); /* ウインドウクラスの名前 */
//GDI+ Start
    GdiplusStartup(&gdip_Token, &gdip_SI, NULL); <----3.プログラムの最初で呼び出す

/* Define window class */
    if( !hPreInst ) /*set up window class and register it */
    {
     if(!Register_WndClass( hInst, szWinName ) )
      return FALSE; /* ウインドウクラスの登録失敗の時 */
    }

/* now create the window */
    if(NULL==(hWnd =Set_Create_Window( hInst ,szWinName , nCmdShow )))
     return FALSE ;

/* begin the message loop */
    while( GetMessage( &lpMsg, NULL, 0, 0 ) )/* begin the message loop */
    {
     TranslateMessage( &lpMsg );
     DispatchMessage( &lpMsg );
    }

//GDI+ 終了処理
    GdiplusShutdown(gdip_Token); <----4.プログラムの最後で呼び出す

   return (int)lpMsg.wParam;
}

/* ++++++++ ウインドウクラス登録用関数 ++++++++ */
BOOL Register_WndClass( HINSTANCE hInst,
                TCHAR *szWinName )

{
   WNDCLASS wc ;//ウインドウクラス構造体( WNDCLASSEXっていうのもあるよ)

    wc.hInstance = hInst; /* Handle to the instans */
    wc.lpszClassName = szWinName; /* name of window class */
    wc.lpfnWndProc = WndProc; /* Window procedure */
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    /* cursol style */
    wc.hIcon = LoadIcon( hInst, NULL );
    /* Large icon style */
    wc.lpszMenuName =NULL; /* Menu define メニューなし */
    wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
    /* Set Window background to white */
    wc.style = 0; /* Window style :0 ->default */
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;

   return RegisterClass( &wc ) ;/* ウインドウクラスの登録 */
}

/* ++++++++ ウインドウ作成関数 ++++++++ */
HWND Set_Create_Window( HINSTANCE hInst ,TCHAR *szWinName ,int nCmdShow)
{
   HWND hWnd; //ウインドウへのハンドル

   /* CW_USEDEFAULT はシステムにお任せという意味だよ */
    hWnd = CreateWindow( szWinName, /* Window Class name */
                   TEXT("てんぷれーと"), /* Title shown in title bar */
                   WS_OVERLAPPEDWINDOW , /* Style of window :Default */
                   CW_USEDEFAULT, /* X cordinate(left of window) */
                   CW_USEDEFAULT, /* Y cordinate(left of window) */
                   CW_USEDEFAULT, /* Width of window */
                   CW_USEDEFAULT, /* Height of window */
                   (HWND)NULL, /* Handle to parent window */
                   (HMENU)NULL, /* Handle to Menu */
                   (HINSTANCE)hInst, /* Instans handle to the program */
                   (LPSTR)NULL ); /* 追加引数なし */

    if(!hWnd )
     return NULL ;

    ShowWindow(hWnd, nCmdShow );

    UpdateWindow( hWnd );

   return hWnd ;

}

/*
ウインドウ手続き関数
*/

LRESULT CALLBACK WndProc( HWND hWnd, UINT messg, /*callback procedure */
                     WPARAM wParam, LPARAM lParam )

{
     //略

}


目次に戻る 画像を表示