Code: Select all
void MainDlg::OnOpen()
{
CFileDialog fileDlg(TRUE, NULL, (CString)"*.dcm");
INT_PTR result = fileDlg.DoModal();
if (result==IDOK)
{
CString str1 = fileDlg.GetPathName();
char *str;
str = str1.GetBuffer(str1.GetLength());
str1.ReleaseBuffer();
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 = w;
bmih.biHeight = -h;
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*3];
dest = (unsigned char*)bits;
const unsigned char* src = new const unsigned char[w*h*3];
src = (BYTE* )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;
delete src;
}
}
}