3

Is there a way to show an IplImage in a picturebox?

I'd like to not save the image and reload it into the picturebox since I need my program to be fast.

I'm using opencv 2.1 in C++. I'm working with Visual Studio 2008. Thank you.

Michal Kottman
  • 16,375
  • 3
  • 47
  • 62
andrea
  • 1,326
  • 7
  • 31
  • 60

1 Answers1

3

This was already discussed here:

IplImage* img=cvLoadImage("sample.jpg",3); // for example

HDC hdc = picturebox.GetDC()->m_hDC;
char m_chBmpBuf[2048];
BITMAPINFO *m_pBmpInfo =0;
m_pBmpInfo = (BITMAPINFO *)m_chBmpBuf;
m_pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_pBmpInfo->bmiHeader.biWidth = img->width;
m_pBmpInfo->bmiHeader.biHeight = -img->height;
m_pBmpInfo->bmiHeader.biBitCount= 24;

m_pBmpInfo->bmiHeader.biPlanes = 1;
m_pBmpInfo->bmiHeader.biCompression = BI_RGB;
m_pBmpInfo->bmiHeader.biSizeImage = 0;
m_pBmpInfo->bmiHeader.biXPelsPerMeter = 0;
m_pBmpInfo->bmiHeader.biYPelsPerMeter = 0;
m_pBmpInfo->bmiHeader.biClrUsed = 0;
m_pBmpInfo->bmiHeader.biClrImportant = 0;

StretchDIBits(hdc, 0, 0, img->width, img->height, 
                   0, 0, img->width, img->height, 
                   img->imageData, m_pBmpInfo,
                   DIB_RGB_COLORS, SRCCOPY);
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • sorry but it does not work.. HDC hdc = picturebox.GetDC()->m_hDC; I can't do this in c++, I tried this way HANDLE handle = (HANDLE)this->PbBoxImg->Handle.ToPointer();//.ToInt32(); HWND hWnd=(HWND)&handle; now i get no errors but nothing is shown in the picture box HDC hdc1 = GetDC(hWnd); – andrea Jan 13 '12 at 16:45
  • I'll take a look during the weekend. I don't have Windows right now. – karlphillip Jan 13 '12 at 16:52
  • 1
    I found the solution: this->pictureBox1->Image=(gcnew System::Drawing::Bitmap(img->width,img->height,img->widthStep, System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr)img->imageData)); don't know if it's the best one, but it works. still really thank you for your help – andrea Jan 16 '12 at 09:13