title: OpenCV Mat of cropped images do not correctly display on MFC View
body: <p>I am using following routines to display OpenCV Mat on MFC View. And it's working well for uncropped images. But for pre-cropped images, it shows blank or weird images like the ones below:</p>
  
  <p>First one is displayed on usual image software such as MS Paint and the second is on my MFC view. Difficult is that uncropped image files are displaying just fine. I am not sure this is fault of SetDIBitsToDevice or OpenCV. But clearly SetDIBitsToDevice loses constant number of bytes in each row. Anybody has any idea to fix this problem?</p>
  
  <p><img src="https://i.stack.imgur.com/vrbSe.jpg" alt="Correct"> <img src="https://i.stack.imgur.com/vE2Ie.jpg" alt="Displayed"></p>
  
  <pre><code>cv::Mat m_cvImage;
  static int Bpp(cv::Mat img) { return 8 * img.channels(); } 
  
  
  void COpenCVTestView::OnDraw(CDC* pDC)
  {
  COpenCVTestDoc* pDoc = GetDocument();
  ASSERT_VALID(pDoc);
  if (!pDoc)
      return;
  if(pDoc-&gt;m_cvImage.empty()) return;
  // TODO: add draw code for native data here
  int height=pDoc-&gt;m_cvImage.rows;
  int width=pDoc-&gt;m_cvImage.cols;
  uchar buffer[sizeof( BITMAPINFOHEADER ) + 1024]; 
  BITMAPINFO* bmi = (BITMAPINFO* )buffer; 
  FillBitmapInfo(bmi,width,height,Bpp(pDoc-&gt;m_cvImage),0);
  SetDIBitsToDevice(pDC-&gt;GetSafeHdc(), 0, 0, width,
      height, 0, 0, 0, height, pDoc-&gt;m_cvImage.data, bmi,
      DIB_RGB_COLORS);
  }
  
  
  void COpenCVTestView::FillBitmapInfo(BITMAPINFO* bmi, int width, int height, int bpp, int origin) 
  { 
  assert(bmi &amp;&amp; width &gt;= 0 &amp;&amp; height &gt;= 0 &amp;&amp; (bpp == 8 || bpp == 24 || bpp == 32)); 
  
  BITMAPINFOHEADER* bmih = &amp;(bmi-&gt;bmiHeader); 
  
  memset(bmih, 0, sizeof(*bmih)); 
  bmih-&gt;biSize = sizeof(BITMAPINFOHEADER); 
  bmih-&gt;biWidth = width; 
  bmih-&gt;biHeight = origin ? abs(height) : -abs(height); 
  bmih-&gt;biPlanes = 1; 
  bmih-&gt;biBitCount = (unsigned short)bpp; 
  bmih-&gt;biCompression = BI_RGB; 
  
  if (bpp == 8) 
  { 
      RGBQUAD* palette = bmi-&gt;bmiColors; 
  
              for (int i = 0; i &lt; 256; i++) 
      { 
          palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i; 
          palette[i].rgbReserved = 0; 
      } 
  } 
  }
  </code></pre>