[Insight-users] ITK VTK MFC - create bitmap from ITK image

Renaud Isabelle renauisa at yahoo.fr
Fri May 20 14:06:19 EDT 2005


Dear users,
 
I have to build an user interface by using MS VC++ for medical image processing. Instead of having to compute reading of image files, I am using ITK-VTK but I am quite new in this.
 
- I read a raw 3D image using ITK and, as you suggested in "http://public.kitware.com/pipermail/insight-users/2003-April/003345.html",  I use itkExtractImageFilter to create a 2D image from the 3D volume and get the pixel data in a buffer with: 
extractFilter->GetOutput()->GetBufferPointer().
 
- I would like to create a bitmap  from this data using MFC classes to show the image in a frame of my user interface already built. To do so, I use HBitmap and CreateDIBitmap, but HBITMAP returned is NULL and nothing happens. 

 
- Can you suggest me a way to use VTK to display my image not in a modal window apart as implemented in vtkImageViewer, but in a frame of my interface?
 
I joined a part of my code below. Please help me.
 
Thanks in advance,
 
Isabelle Renaud
 
//-------code----------------------
 if(extension == "raw")
 {
   //declarations
  typedef itk::Image< short, 3> ImageType;
  typedef itk::ImageFileReader< ImageType > ReaderType;
    
  //initialisations
  ReaderType::Pointer reader = ReaderType::New();
   
  //provide minimum info
  reader->SetFileName("myImage.mhd");
 
  try
  {
   reader->Update();
  }
  catch(itk::ExceptionObject &exception)
  {
   CString msg;
   msg.Format("Erreur: %s", exception.GetDescription());
   AfxMessageBox(msg, MB_ICONERROR);
  }
 
  //Create a device context to load the bitmap into
  CPaintDC dc(this); 
  dcMem.CreateCompatibleDC(&dc);
  if(GrabRAWFrame(1) != NULL) Invalidate();
  else AfxMessageBox("erreur a creation de image BMP\n");
 }
}
 
HBITMAP CSequenceAvi::GrabRAWFrame(int frame)
{
 //declarations
 typedef short PixelType;
 typedef itk::Image< PixelType, 2 > ImageType2D;
 typedef itk::Image< PixelType, 3 > ImageType3D;
  
 typedef itk::ImageFileReader< ImageType3D > ReaderType;
 typedef itk::ExtractImageFilter< ImageType3D, ImageType2D > ExtractType;
 
 //initialisations
 ReaderType::Pointer reader = ReaderType::New();
 ExtractType::Pointer extractFilter = ExtractType::New();
 reader->SetFileName("myImage.mhd");
 reader->Update();
 ImageType3D::RegionType inputRegion = 
  reader->GetOutput()->GetLargestPossibleRegion();
 ImageType3D::SizeType size = inputRegion.GetSize();
 size[2] = 0;
 ImageType3D::IndexType start = inputRegion.GetIndex();
 start[2] = frame;
 //creation of an image region
 ImageType3D::RegionType desiredRegion;
 desiredRegion.SetSize(size);
 desiredRegion.SetIndex(start);
 //region passed to filter to extract
 extractFilter->SetExtractionRegion(desiredRegion);
 //connect an d execute
 extractFilter->SetInput(reader->GetOutput());
 try{
  extractFilter->Update();
 }
 catch(itk::ExceptionObject &exception)
 {
  CString msg;
  msg.Format("Erreur: %s", exception.GetDescription());
  AfxMessageBox(msg, MB_ICONERROR);
 }
 
 //get the data
 ImageType2D::Pointer image = extractFilter->GetOutput();
 short* pData = extractFilter->GetOutput()->GetBufferPointer();
 
 BITMAPINFOHEADER bih;
 memset(&bih, 0, sizeof(BITMAPINFOHEADER));
 bih.biBitCount=24; //16 bits per pixel
 bih.biClrImportant=0;
 bih.biClrUsed=0;
 bih.biCompression=BI_RGB;//uncompressed format
 bih.biPlanes=1;//always
 bih.biSize= 40; //nb of bytes required by the structure BITMAPINFOHEADER
 bih.biWidth=width;
 bih.biHeight=height;
 bih.biXPelsPerMeter=0;
 bih.biYPelsPerMeter=0;
 bih.biSizeImage=((((bih.biWidth * bih.biBitCount)+31)& ~31) >>3)*bih.biHeight;
 //0 for uncompressed images
 
 //on cree l image
 CPaintDC dc(this);
 HBITMAP hBitmap = CreateDIBitmap(dc,//handle of device context
     (BITMAPINFOHEADER*)&bih,//address of bitmap size and format data
     CBM_INIT,//initialization flag
     pData,//address of initialization data
     (BITMAPINFO*)&bih,//address of bitmap color format data
     DIB_RGB_COLORS);//color data usage
 return hBitmap;
}

void CSequenceAvi::OnPaint() 
{
 CPaintDC dc(this); // device context for painting
 
 // TODO: Add your message handler code here
 ShowBitmap(&dc);
 //Draw rectangle
 SetPenBrush(&dc);
 dc.Rectangle(rect_roi);
 ResetPenBrush(&dc);
}

void CSequenceAvi::ShowBitmap(CPaintDC *pDC)
{
   //Sélectionner 1 nouvel objet en memoire, détruire l’ancien 
 HGDIOBJ pOldObj = SelectObject(dcMem,hBitmap);
 if(pOldObj) DeleteObject(pOldObj);
 
 //draw avi file in a fixed rectangle
 CRect rect;
 GetDlgItem(IDC_RECT)->GetWindowRect(rect);
 ScreenToClient(rect);//convert coord of rect on screen to client area coord
 
 //move the bitmap from the memory to the screen
 pDC->StretchBlt(0,0,rect.Width(),rect.Height(),
        &dcMem, 0,0,
        width, height,SRCCOPY);
}



		
---------------------------------
 Découvrez le nouveau Yahoo! Mail : 1 Go d'espace de stockage pour vos mails, photos et vidéos !
Créez votre Yahoo! Mail  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20050520/9a4f68b3/attachment.html


More information about the Insight-users mailing list