Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hi I want to open camera in windows forms in cpp.

I have an application to load images from files for processing, but I need to load live images from a camera. I edited the code but I am getting errors. Here I cite the original code and my edited version.

            Older Code

           private: System::Void btnLoad_Click(System::Object^  sender, System::EventArgs^  e) {

    OpenFileDialog^ dgOpen = gcnew OpenFileDialog();
    dgOpen->Filter = "Image(*.bmp; *.jpg)|*.bmp;*.jpg|All files (*.*)|*.*||";
    if (dgOpen->ShowDialog() == System::Windows::Forms::DialogResult::Cancel)
    {
        return;
    }
    Bitmap^ bmpSrc = gcnew Bitmap(dgOpen->FileName);
    ptbSrc->Image = bmpSrc;
    ptbSrc->Refresh();
    srcImg = imread(stringTochar.ConvertString2Char(dgOpen->FileName));
    int dbSize_H = srcImg.rows;
    int dbSize_W = srcImg.cols;
    System::String^ h = gcnew System::String(to_string(dbSize_H).c_str());
    System::String^ w = gcnew System::String(to_string(dbSize_W).c_str());

    imgSize->Text = w + "x" + h;}

         Edited code


        //CvCapture* capture;
        VideoCapture capture;
         Mat frame, srcImg;
        private: System::Void btnLoad_Click(System::Object^  sender, System::EventArgs^  e) {

        capture.open(0);
        if (capture.isOpened())
        capture.read(frame);

    ptbSrc->Image = gcnew;
          System::Drawing::Bitmap(frame->width,frame->height,frame->widthStep,
          System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) frame->imageData);
    ptbSrc->Refresh();
    srcImg = frame;
    int dbSize_H = srcImg.rows;
    int dbSize_W = srcImg.cols;
    System::String^ h = gcnew System::String(to_string(dbSize_H).c_str());
    System::String^ w = gcnew System::String(to_string(dbSize_W).c_str());

    imgSize->Text = w + "x" + h;}

Here frame is for picture box 'ptbSrc' and srcImg for further processing.

Hi I want to open camera in windows forms in cpp.

I have an application to load images from files for processing, but I need to load live images from a camera. I edited the code but I am getting errors. Here I cite the original code and my edited version.version. Opencv 3.1.

            Older Code

           private: System::Void btnLoad_Click(System::Object^  sender, System::EventArgs^  e) {

    OpenFileDialog^ dgOpen = gcnew OpenFileDialog();
    dgOpen->Filter = "Image(*.bmp; *.jpg)|*.bmp;*.jpg|All files (*.*)|*.*||";
    if (dgOpen->ShowDialog() == System::Windows::Forms::DialogResult::Cancel)
    {
        return;
    }
    Bitmap^ bmpSrc = gcnew Bitmap(dgOpen->FileName);
    ptbSrc->Image = bmpSrc;
    ptbSrc->Refresh();
    srcImg = imread(stringTochar.ConvertString2Char(dgOpen->FileName));
    int dbSize_H = srcImg.rows;
    int dbSize_W = srcImg.cols;
    System::String^ h = gcnew System::String(to_string(dbSize_H).c_str());
    System::String^ w = gcnew System::String(to_string(dbSize_W).c_str());

    imgSize->Text = w + "x" + h;}

         Edited code


        //CvCapture* capture;
        VideoCapture capture;
         Mat frame, srcImg;
        private: System::Void btnLoad_Click(System::Object^  sender, System::EventArgs^  e) {

        capture.open(0);
        if (capture.isOpened())
        capture.read(frame);

    ptbSrc->Image = gcnew;
          System::Drawing::Bitmap(frame->width,frame->height,frame->widthStep,
          System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) frame->imageData);
    ptbSrc->Refresh();
    srcImg = frame;
    int dbSize_H = srcImg.rows;
    int dbSize_W = srcImg.cols;
    System::String^ h = gcnew System::String(to_string(dbSize_H).c_str());
    System::String^ w = gcnew System::String(to_string(dbSize_W).c_str());

    imgSize->Text = w + "x" + h;}

Here frame is for picture box 'ptbSrc' and srcImg for further processing.