2012-10-22 65 views
0

我做了一個儀表板,顯示相機的強度。我的儀表板繪圖功能是...十字標記正在圖片框

void drawImageMeter(float intensity_value,short * cambuff) 
{ 

    //Read Image function*********************** 
       IplImage *Image; 
         FILE * fp; 
         fp=fopen("Imagebuffer.bin","rb"); 
         short* Pattern_Data = new short[width*height]; 
         fread(Pattern_Data,sizeof(short),width*height,fp); 
         fclose(fp); 


       Image=cvCreateImage(cvSize(width, height), IPL_DEPTH_16S, 1); 
       cvSetData(Image,Pattern_Data, sizeof(short)*width); 
       CvPoint minIntensityLoc, maxIntensityLoc; 
       double maxIntensity,minIntensity; 

       cvMinMaxLoc(Image, &minIntensity, &maxIntensity, &minIntensityLoc, &maxIntensityLoc, NULL); 
       CvPoint2D32f roiStart; 
       CvPoint2D32f roiEnd; 
       roiStart.x =(width/2)-500; 
       roiStart.y = 0; 
       roiEnd.x = (width/2)+500; 
       roiEnd.y = height; 

       int roiWidth; 
       int roiHeight; 
       roiWidth = roiEnd.x - roiStart.x; 
       roiHeight = roiEnd.y - roiStart.y; 
       cvSetImageROI(Image, cvRect(roiStart.x, roiStart.y, roiWidth, roiHeight)); 

       IplImage *croped_image = cvCreateImage(cvGetSize(Image), IPL_DEPTH_16S,1); 
       cvCopy(Image, croped_image, NULL); 
       cvResetImageROI(Image); 
       //cvSaveImage("roiIMage.bmp",croped_image); 
       CvScalar result = cvSum(croped_image); 

       double sum = result.val[0]; 
       double average = sum/(roiWidth * roiHeight); 
       int max_min = maxIntensity - minIntensity; 

       Intensity_Values values = {maxIntensity,minIntensity,average,max_min}; 
       cvReleaseImage(&croped_image); 
       cvReleaseImage(&Image); 
       free(Pattern_Data); 

         if(count1 == 1) 
         { 
          count1++; 
          FocusExposure::label7->Text = Convert::ToString(maxIntensity); 
         } 
         FocusExposure::label14->Text = Convert::ToString(maxIntensity); 

        FocusExposure::label8->Text = Convert::ToString(minIntensity); 
        FocusExposure::label9->Text = Convert::ToString(average); 
        FocusExposure::label10->Text = Convert::ToString(max_min); 
    //****************************************** 




     IplImage *Background=cvLoadImage("Dialer.bmp", 1); 
     int width,height; 
     width=Background->width; 
     height=Background->height; 
     if(counter==1) 
     { 
      counter++; 
     needle_center.x=width/2; 
     needle_center.y=height/2; 


     needle_top.x=needle_center.x; 
     needle_top.y=needle_center.y-140; 
     } 


      double const PI = 3.14159265358979323; 
      int x1 = needle_top.x; 
      int y1 = needle_top.y; 

      int x0=needle_center.x; 
      int y0=needle_center.y; 
      float angle; 

      CurrIntensity = maxIntensity; 
      angle = CurrIntensity-PreIntensity; 
      angle= 0.0703125f * angle; 






      // degrees, not radians 
      float radians = angle * (PI/180.0f); // convert degrees to radians 

      if (current_max==1) 
      { 
       current_max++; 
       int N1x1 = needle_top.x; 
       int N1y1 = needle_top.y; 
       needle1_top.x = ((N1x1-x0) * cos(radians)) - ((N1y1-y0) * sin(radians)) + x0; 
       needle1_top.y = ((N1x1-x0) * sin(radians)) + ((N1y1-y0) * cos(radians)) + y0; 
      } 
      needle_top.x = ((x1-x0) * cos(radians)) - ((y1-y0) * sin(radians)) + x0; 
      needle_top.y = ((x1-x0) * sin(radians)) + ((y1-y0) * cos(radians)) + y0; 

      cvLine(Background, needle_center, needle1_top, CV_RGB(0, 0, 255), 1, 4, 0); 

      cvLine(Background, needle_center, needle_top, CV_RGB(255, 0, 0), 1, 4, 0); 
      FocusExposure::pictureBox1->Image= nullptr; 
      FocusExposure::pictureBox1->Refresh(); 
     System::Drawing::Bitmap ^bmp = gcnew System::Drawing::Bitmap(Background->width,Background->height,Background->widthStep,System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr)Background->imageData); 
     System::IntPtr hbitmap = bmp->GetHbitmap(); 

     FocusExposure::pictureBox1->Image=FocusExposure::pictureBox1->Image->FromHbitmap(hbitmap); 
     delete bmp; 
     DeleteObject((HGDIOBJ)hbitmap); 


     PreIntensity = CurrIntensity; 
     cvReleaseImage(&Background); 

} 

在此函數中沒有內存泄漏。當應用程序啓動時,它運行良好,但過了一段時間後,儀表可以釋放一些針頭,然後紅色十字標記出現在pictureBox中,然後出現異常。

我不是爲什麼這個異常來了。我也搜索了谷歌有關但沒有找到任何解決方案。

異常而來的是...

************** Exception Text ************** 
System.ArgumentException: Parameter is not valid. 
    at System.Drawing.Image.get_RawFormat() 
    at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height) 
    at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect) 
    at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe) 
    at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer) 
    at System.Windows.Forms.Control.WmPaint(Message& m) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 


************** Loaded Assemblies ************** 
mscorlib 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.269 (RTMGDR.030319-2600) 
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll 
---------------------------------------- 
PUMA_LED_TESTER 
    Assembly Version: 1.0.4673.19390 
    Win32 Version: 
    CodeBase: file:///D:/VS/FK/Puma_15_10_2012/PUMA_LED_TESTER/Release/PUMA_LED_TESTER.exe 
---------------------------------------- 
System 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.269 built by: RTMGDR 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll 
---------------------------------------- 
System.Windows.Forms 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.278 built by: RTMGDR 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll 
---------------------------------------- 
System.Drawing 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.282 built by: RTMGDR 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll 
---------------------------------------- 

************** JIT Debugging ************** 
To enable just-in-time (JIT) debugging, the .config file for this 
application or computer (machine.config) must have the 
jitDebugging value set in the system.windows.forms section. 
The application must also be compiled with debugging 
enabled. 

請幫我理清這個問題。

謝謝。

回答

2
 delete bmp; 

不要銷燬已分配給PictureBox :: Image屬性的位圖。 GDI +在嘗試繪製已銷燬的位圖時會急轉直下。與HBITMAP的修修補補也不合適,應該改爲類似於此:

 // FocusExposure::pictureBox1->Image= nullptr; Not this but: 
    delete FocusExposure::pictureBox1->Image; 
    FocusExposure::pictureBox1->Image = gcnew System::Drawing::Bitmap(...); 
    PreIntensity = CurrIntensity; 
    // etc.. 
+0

謝謝你的答覆......我已經編輯我的代碼,你告訴我。我已經評論了這些行,但同樣的問題正在... 已評論行... // FocusExposure :: pictureBox1-> Image = nullptr; // FocusExposure :: pictureBox1-> Refresh(); //刪除bmp; – geeta

+0

好吧,這看起來不像我推薦你使用的代碼。不知道你期待我如何幫助你,我無法從這裏看到你的肩膀。處理較小的項目以獲得編寫好代碼的感覺,您需要練習。 –

+0

對不起,我還沒有看到你有什麼建議..我再次更改了代碼... ' 刪除FocusExposure :: pictureBox1-> Image; FocusExposure :: pictureBox1-> Image = gcnew System :: Drawing :: Bitmap(Background-> width,Background-> height,Background-> widthStep,System :: Drawing :: Imaging :: PixelFormat :: Format24bppRgb,(System :: IntPtr的)背景 - >的imageData); PreIntensity = CurrIntensity;' 但現在應用程序啓動時它什麼也沒有顯示,不發送錯誤是comimg和應用程序關閉。 請給出任何建議。 – geeta