Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to convert byte array to hbitmap and vice versa
i saw the function
CreateDIBitmap( );
StretchBlt( );

but image quality is some what disturbed.

wel is there any good way .
Posted
Comments
pranav_30 25-Feb-12 11:11am    
using open cv i can get iplimage from byte array with help of cvGet2D() &cvSet2D()
then convert thay iplimage into hbitmap
is this proper way ??

Use GDI plus if possible.( it has interpolation)
You can use Bitmap class, then use LockBits function , and copy your pixels in to it. You will also get native bitmap handle from Bitmap class(use GetHbitmap function)..
 
Share this answer
 
Comments
pranav_30 25-Feb-12 10:57am    
i think gdiplus have some performance issue.?????
jk chan 26-Feb-12 7:33am    
not much(how much frame rate you want ? ) ..
C++
#include "stdafx.h"
#include "Resample.h" // this is dll for resizing the image

class imageReader
{

public:
	imageReader();
	~imageReader();

	HBITMAP readBuffer(char *buffer, HWND parentWindow, RECT rectDisp);
	void renderImage(HBITMAP disimage);

private:
	HWND parent;
	RECT rect;
	HBITMAP image;
	
};
imageReader::imageReader()
{

}
imageReader::~imageReader()
{

}
HBITMAP imageReader::readBuffer(char *buffer, HWND parentWindow, RECT rectDisp)
{
	parent=parentWindow;
	rect=rectDisp;

	BITMAPFILEHEADER& bfh = (BITMAPFILEHEADER&)buffer[0];
	BITMAPINFO& bi = (BITMAPINFO&)buffer[sizeof(BITMAPFILEHEADER)];
	BITMAPINFOHEADER& bih = bi.bmiHeader; 
	char* bitmap = &buffer[bfh.bfOffBits];

	HDC hdcW = GetDC(parent); // window's DC
	image = CreateDIBitmap( hdcW, &bih, CBM_INIT, bitmap, &bi, DIB_RGB_COLORS );          
	image= CreateResampledBitmap(hdcW, image, rect.right-rect.left, rect.bottom-rect.top, STOCK_FILTER_BOX);
return image;
}

void imageReader::renderImage(HBITMAP disimage)
{
	PAINTSTRUCT ps;

	HDC hdc = BeginPaint(parent, &ps);
	BITMAP bm;
	HDC hdcMem = CreateCompatibleDC(hdc);
	SelectObject(hdcMem, disimage);
	GetObject(disimage, sizeof(bm), &bm);

	BitBlt(hdc,rect.left, rect.top, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
	DeleteDC(hdcMem);
		
	EndPaint( parent, &ps );

}



// example of implementation of above class


//imageReader  imageobj;
// FILE* f = fopen( "C:\\test.bmp", "rb" ); if( f==0 ) return 1;
//  fread( buf, 1,sizeof(buf), f );
//  fclose(f);
//}

//  WINDOWPLACEMENT lpwndpl2;
//  GetWindowPlacement(hw,&lpwndpl2); //hw is handle to picture control where u want to render image
//  RECT rectImage;
//  rectImage.left=lpwndpl2.rcNormalPosition.left;
//  rectImage.right=lpwndpl2.rcNormalPosition.right;
//  rectImage.top=lpwndpl2.rcNormalPosition.top;
//  rectImage.bottom=lpwndpl2.rcNormalPosition.bottom;

//  HBITMAP hBitmap=imageobj.readBuffer(buf,hwparent,rectImage);//hwparent is handle to main dialog(parent)
/
//imageobj.renderImage(hBitmap);
//InvalidateRect(hwparent , &rectImage, TRUE);



// add imageobj.renderImage(hBitmap); in wm_paint
 
Share this answer
 
v2
Comments
Richard MacCutchan 26-Feb-12 12:11pm    
Please format your code snippets correctly.
pranav_30 26-Feb-12 13:16pm    
i tried to format the snippet but it wont work...
well above code works fine for me. but i am new to c++, so if any budy have any suggestion then plz post the suggestion....
thanks.
Mohibur Rashid 26-Feb-12 20:13pm    
To show the code format as code format you should uncheck the check box below the editor.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900