How to get a Bitmap object in VS from ImageFileReader object?

Hi.

How to get a Bitmap object like CBitmap in VS from ImageFileReader object?
I can write the image obtained from ImageFileReader into a BMP file and then load it using LoadBitmap(…) function of Visual Studio and then show it in the Picture Control object. But I wish not to save in BMP file for load it after, instead I need to get the BMP image object in memory not at disk. I was searching on Internet but I do not found anything that fits me.

Thanks in advance

Hello @Armando,

As you didn’t provide details I’ll assume you are working in C++ and using ITK.

You can access the Image’s memory buffer using the GetBufferPointer and I suspect you can copy the data over to your BMP data structure, don’t know its API so just assuming here.

Possibly the following SimpleITK code may be relevant (likely not, but just in case):

def sitkimage_2_qpixmap(self, sitk_image):
        """
        Convert a SimpleITK.Image to Qt's QPixmap (PySide2/Qt5). Works with grayscale or a three
        channel image where we assume that it represents values in the RGB color space.
        In SimpleITK there is no notion of color space, so if the three channels were in
        HSV colorspace the display will look strange.
        """
        if (
            sitk_image.GetNumberOfComponentsPerPixel() == 1
            and sitk_image.GetPixelID() != sitk.sitkUInt8
        ):
            sitk_image = sitk.Cast(sitk.RescaleIntensity(sitk_image), sitk.sitkUInt8)
        arr = sitk.GetArrayViewFromImage(sitk_image)
        return QPixmap.fromImage(
            QImage(
                arr.data.tobytes(),
                sitk_image.GetWidth(),
                sitk_image.GetHeight(),
                arr.strides[0],  # number of bytes per row
                QImage.Format_Grayscale8
                if sitk_image.GetNumberOfComponentsPerPixel() == 1
                else QImage.Format_RGB888,
            )
        )

Hi Ziv.

Thank you very much for your answer. Yes I am working in C++ with Visual Studio in an MFC application. I am close to the final solution right now, but still remain some difficulties, it shows the image four times in the same image instead a single one. Here is my code and in the attachment is the output of the PictureControl in the Dialog:

typedef unsigned char PixelType; 
const unsigned int Dimension = 3;
typedef itk::Image< PixelType, Dimension > InputImageType;
typedef itk::ImageFileReader< InputImageType > ReaderType;

ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName("E:\\0.dcm");
reader->Update();

InputImageType::SizeType size; 
size[0] = 512; size[1] = 512;

InputImageType::IndexType start; 
start[0] = 0; start[1] = 0;

InputImageType::RegionType region; 
region.SetSize(size); region.SetIndex(start);

InputImageType::Pointer image = reader->GetOutput();
image->SetRegions(region); 
image->Allocate();

PixelType* pixelData = image->GetBufferPointer();

LPBITMAP lpBitmap = new BITMAP;
lpBitmap->bmType = 0;
lpBitmap->bmWidth = 512;
lpBitmap->bmHeight = 512;
lpBitmap->bmWidthBytes = 2048;
lpBitmap->bmPlanes = 1;
lpBitmap->bmBitsPixel = 32;
lpBitmap->bmBits = pixelData;

CBitmap mybitmap4;
mybitmap4.CreateBitmapIndirect(lpBitmap);
PictureControl->SetBitmap(mybitmap4.operator HBITMAP());

PictureControl_Output.bmp (2.2 MB)
PictureControl_Example.cpp (1.1 KB)

By looking at BITMAP docs, you probably need to change lpBitmap->bmBitsPixel = 32; from 32 to 8.

Also reading a DICOM file with 8-bit depth will clip the values causing “noise” to appear instead of higher intensities. If bitmaps can show 16-bit grayscale images, you should rescale your image from 16-bit to 8-bit before converting it to bitmap.