Link to home
Start Free TrialLog in
Avatar of Алексей Димитриев
Алексей Димитриев

asked on

Display an image in the Picture Control from pixel data

Good day! I'm trying to display an image in the Picture Control in the MFC with pixel data. Tell me what's wrong.

image = new DicomImage(str);
		
		image->setMinMaxWindow();
		Uint8 *pixelData = (Uint8 *)(image->getOutputData(8));
		if (pixelData != NULL){
			int w = image->getWidth();
			int h = image->getHeight();

			HBITMAP hBitmap = NULL;

			BITMAPINFOHEADER bmih;
			bmih.biSize = sizeof(BITMAPINFOHEADER);
			bmih.biWidth = 1024;
			bmih.biHeight = -512;
			bmih.biPlanes = 1;
			bmih.biBitCount = 8;
			bmih.biCompression = BI_RGB ;
			bmih.biSizeImage = 0;
			bmih.biXPelsPerMeter = 10;
			bmih.biYPelsPerMeter = 10;
			bmih.biClrUsed = 0;
			bmih.biClrImportant = 0;

			BITMAPINFO dbmi;
			ZeroMemory(&dbmi, sizeof(dbmi));  
			dbmi.bmiHeader = bmih;
			dbmi.bmiColors[0].rgbBlue = 0;
			dbmi.bmiColors[0].rgbGreen = 0;
			dbmi.bmiColors[0].rgbRed = 0;
			dbmi.bmiColors[0].rgbReserved = 0;
			void* bits = (void*)&(pixelData[0]); 

			// Create DIB
			HDC localDC = ::GetDC(NULL);
			hBitmap = CreateDIBSection(localDC, &dbmi, DIB_RGB_COLORS, &bits, NULL, 0);
			if (hBitmap == NULL) {
				::MessageBox(NULL, __T("Could not load the desired image image"), __T("Error"), MB_OK);
				return;
			}

			unsigned char* dest = new unsigned char[w*h];
			dest =	(unsigned char*)bits;
			const unsigned char* src  = pixelData;
			for (int j=0; j<w; j++){
				for (int i=0; i<h; i++, src++){
					*dest++ = *src;
					*dest++ = *src;
					*dest++ = *src;
				}
			}	

			m_picture.SetBitmap(hBitmap);
			delete dest;

Open in new window


Thank you!
Avatar of romanm
romanm

First, are you sure the image loaded is RGB? 3bytes per pixel.
Second, how about color order in the source?! RGB or BGR?

And third, although it should be the first question, What do you get? What is wrong when you run it?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.