Drawing a mat picture not correct in hdc of C++

Hi,
I create a function like that (below)in C++ to draw a mat picture to HDC. I have use imshow to check and both image and cvImgTmp has been show correctly.
But my final picture look like that
image
I think I do something wrong with “bitInfo” or “SetDIBitsToDevice”
Are there anyone can help me to fix this.

Thank you


void DrawMatToHDC(const char* DicomPath, HDC hdc, RECT rc)// Developing
	{
		DicomImage DCM_image(DicomPath);
		DCM_image.setWindow(0);
		cv::Mat image(int(DCM_image.getWidth()), int(DCM_image.getHeight()), CV_8U, (uchar*)DCM_image.getOutputData(8));

		cv::Size winSize(rc.right, rc.bottom);
		//imshow("DICOM Image Example", image);
		// Resize the source to the size of the destination image if necessary
		cv::Mat cvImgTmp(winSize, CV_8UC3);
		if (image.size() != winSize)
		{
			cv::resize(image, cvImgTmp, winSize);
			
		}
		else
		{
			cvImgTmp = image.clone();
		}
		//imshow("DICOM Image Example", cvImgTmp);
		// Rotate the image
		cv::flip(cvImgTmp, cvImgTmp, 0);

		// Initialize the BITMAPINFO structure
		BITMAPINFO bitInfo;
		bitInfo.bmiHeader.biBitCount = 24;
		bitInfo.bmiHeader.biWidth = winSize.width;
		bitInfo.bmiHeader.biHeight = winSize.height;
		bitInfo.bmiHeader.biPlanes = 1;
		bitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		bitInfo.bmiHeader.biCompression = BI_RGB;
		bitInfo.bmiHeader.biClrImportant = 0;
		bitInfo.bmiHeader.biClrUsed = 0;
		bitInfo.bmiHeader.biSizeImage = 0;
		bitInfo.bmiHeader.biXPelsPerMeter = 0;
		bitInfo.bmiHeader.biYPelsPerMeter = 0;
		//
		/*BITMAPINFOHEADER bi = { sizeof(bi) };
		bi.biWidth = cvImgTmp.cols;
		bi.biHeight = -cvImgTmp.rows;
		bi.biBitCount = (WORD)(cvImgTmp.channels()*13);
		bi.biPlanes = 1;*/

		// Add header and OPENCV image's data to the HDC
		SetDIBitsToDevice(hdc, 0, 0, cvImgTmp.cols, cvImgTmp.rows, 0, 0, 0, cvImgTmp.rows,
			cvImgTmp.data, &bitInfo, DIB_RGB_COLORS);
		/*SetDIBitsToDevice(hdc, 0, 0, cvImgTmp.cols, cvImgTmp.rows, 0, 0, 0, cvImgTmp.rows,
			cvImgTmp.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);*/
	}

width / height swapped here.
also: you try to show a 24bit image, but the dicom input is 8bit, single channel only ? if it indeed has 3 channels, you need a CV_8UC3 flag there.

dont pre-allocate this,
it will get overwritten anyway, and wont have the type / size you expect !

Hi,
Thank you for your reply. I have change as you recommend and it is still not correct
image

there is always more than 1 error …

And actually, I dont understand your second point, could you please explain it.
Thank you

do you have a color image or a grayscale image?

if you have a single answer to that question, you need to analyze your code more thoroughly.

Some this this is color and some time this is just black and white. So it depended. But actually my question is more about the size of the picture. Do you have any idea for that?. Thank you