Advertisement
dan-masek

Displaying cv::Mat with WinAPI/WTL

Apr 5th, 2016
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. void image_scan_display::draw_current_image(CDCHandle& dc)
  2. {
  3.     cv::Mat image(processor_.get_canvas());
  4.  
  5.     CBitmap bmp;
  6.     create_bitmap_rgb(dc, bmp, image.cols, image.rows, image.ptr());
  7.  
  8.     CDCHandle mem_dc;
  9.     mem_dc.CreateCompatibleDC(dc);
  10.     HBITMAP old_bmp(mem_dc.SelectBitmap(bmp));
  11.  
  12.     dc.BitBlt(layout_.image.left
  13.         , layout_.image.top
  14.         , image.cols
  15.         , image.rows
  16.         , mem_dc
  17.         , 0
  18.         , 0
  19.         , SRCCOPY);
  20.     mem_dc.SelectBitmap(old_bmp);
  21.     mem_dc.DeleteDC();
  22.  
  23.     if (draw_selection_) {
  24.         dc.InvertRect(CRect(mouse_down_point_, mouse_up_point_));
  25.         dc.FrameRect(CRect(mouse_down_point_, mouse_up_point_)
  26.             , AtlGetStockBrush(WHITE_BRUSH));
  27.     }
  28. }
  29.  
  30. // ------------------------------------------------------------------------------------------------
  31.  
  32. inline void create_bitmap_rgb(CDCHandle& dc
  33.     , CBitmap& bmp
  34.     , LONG width
  35.     , LONG height
  36.     , uint8_t const* data)
  37. {
  38.     BITMAPINFO bi = { 0 };
  39.     bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  40.     bi.bmiHeader.biWidth = static_cast<LONG>(width);
  41.     bi.bmiHeader.biHeight = static_cast<LONG>(-height);
  42.     bi.bmiHeader.biPlanes = 1;
  43.     bi.bmiHeader.biBitCount = 24;
  44.     bi.bmiHeader.biCompression = BI_RGB;
  45.  
  46.     bmp.CreateDIBitmap(dc
  47.         , &bi.bmiHeader
  48.         , CBM_INIT
  49.         , data
  50.         , &bi
  51.         , DIB_RGB_COLORS);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement