Pelles C forum

C language => Beginner questions => Topic started by: Grincheux on March 23, 2016, 09:08:35 am

Title: Transforming an image to a HBITMAP
Post by: Grincheux on March 23, 2016, 09:08:35 am
When you use StretchDIBits you must give it the image bits. Sometimes you need to transform this image bits to a bitmap, that is the goal of this function.

The function needs this structure :

Code: [Select]
typedef struct tagIMAGEINFOS
{
LPBYTE lpImageBits ;
HBITMAP hImgBitmap ;
BITMAPINFO BitmapInfo ;

char Dummy[(sizeof(BITMAPV5HEADER ) - sizeof(BITMAPINFOHEADER))] ;
char szImageFile[MAX_PATH + 4] ;
BYTE FreeSpace[256] ;
} IMAGEINFOS, *LPIMAGEINFOS ;

I hope this will help you playing with images.

Code: [Select]
HBITMAP ImageToBitmap(LPIMAGEINFOS __lpImage)
{
HDC _hDC, _hDCMem1 ;
HBITMAP _hBmp1, _hBmp1Old ;

_hBmp1 = NULL ;

if(!__lpImage) return (NULL) ;

_hDC = GetDC(NULL) ;
if(_hDC)
{
_hDCMem1 = CreateCompatibleDC(_hDC) ;
if(_hDCMem1)
{
SetStretchBltMode(_hDCMem1,COLORONCOLOR) ;

_hBmp1 = CreateCompatibleBitmap(_hDC,__lpImage->BitmapInfo.bmiHeader.biWidth,__lpImage->BitmapInfo.bmiHeader.biHeight) ;
if(_hBmp1)
{
_hBmp1Old = SelectObject(_hDCMem1,_hBmp1) ;
if(_hBmp1Old)
{
if(StretchDIBits(_hDCMem1,0,0,__lpImage->BitmapInfo.bmiHeader.biWidth,__lpImage->BitmapInfo.bmiHeader.biHeight,0,0,__lpImage->BitmapInfo.bmiHeader.biWidth,__lpImage->BitmapInfo.bmiHeader.biHeight,__lpImage->lpImageBits,&__lpImage->BitmapInfo,DIB_RGB_COLORS,SRCCOPY))
{
SelectObject(_hDCMem1,_hBmp1Old) ;
DeleteDC(_hDCMem1) ;
ReleaseDC(NULL,_hDC) ;
return (_hBmp1) ;
}

SelectObject(_hDCMem1,_hBmp1Old) ;
}

DeleteObject(_hBmp1) ;
}

DeleteDC(_hDCMem1) ;
}

ReleaseDC(NULL,_hDC) ;
}

return (0) ;
}