January 12, 2012

Read image from file in win32 using gdiplus

include : gdiplus.h
library : gdiplus.lib

// initialize in the begging of application
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR hgdiplusToken;
Gdiplus::GdiplusStartup ( &hgdiplusToken, &gdiplusStartupInput, NULL );


// read image from file
Gdiplus::Bitmap *bitmap = new Gdiplus::Bitmap ( L"filename.png" );
int width = bitmap->GetWidth();
int height = bitmap->GetHeight();
Gdiplus::Rect rt ( 0, 0, width, height );
unsigned char *image = new unsigned char[width*height*4];
Gdiplus::BitmapData data;
bitmap->LockBits ( &rt, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &data );
memcpy ( image, data.Scan0, sizeof ( unsigned char ) * width * height * 4 );
bitmap->UnlockBits ( &data );
delete bitmap;
bitmap = NULL;


// release in the end of application
delete[] image;
Gdiplus::GdiplusShutdown ( hgdiplusToken );

No comments:

Post a Comment