none
請教攫取影像問題 RRS feed

解答

  • Q:利用此指標將圖存在一陣列中或BitMap中 ?

     

    A:

    Code Snippet

     


        frmSnapForm.SnapImage.Image = Image.FromHbitmap((System.IntPtr)e.frameBitmapHandle);


            
            Bitmap bmp = frmSnapForm.SnapImage.Image ;

            // Lock the bitmap's bits. 
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bmp.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int bytes  = bmpData.Stride * bmp.Height;
         byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

            // Set every third value to 255. A 24bpp bitmap will look red. 
            for (int counter = 2; counter < rgbValues.Length; counter += 3)
                rgbValues[counter] = 255;

            // Copy the RGB values back to the bitmap
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

            // Unlock the bits.
            bmp.UnlockBits(bmpData);

              

     

     

     

     

    2008年11月15日 上午 07:06

所有回覆

  • 有何問題嗎 ?

    2008年11月12日 上午 01:18
  •  

    請問他擷取到圖後會會觸發 下面這函式

    private void axVideoGrabberNET1_OnFrameCaptureCompleted(object sender, Axvidgrab_NET.IVideoGrabberNETEvents_OnFrameCaptureCompletedEvent e)

    {

                if (e.destType == vidgrab_NET.TxFrameCaptureDest.fc_TBitmap)
                    {
                    if (!frmSnapForm.Visible)
                        {
                        frmSnapForm.Show();
                        }
                    if (frmSnapForm.SnapImage.Image != null)
                        {
                        frmSnapForm.SnapImage.Image.Dispose();
                        }
                    frmSnapForm.SnapImage.Image = Image.FromHbitmap((System.IntPtr)e.frameBitmapHandle);
                    }
                }

    }

     

    其中圖就是存在 e這裡面,這裡面有個frmeBitMapHandle這應該是他擷取到圖的第一位置的指標,但C#中如何利用此指標將圖存在一陣列中或BitMap中 ?

    2008年11月15日 上午 05:22
  • For Delphi and C++Buidler versions, the video frame is returned as TBitmap (from the "graphics" unit).
    For OCX versions the video frame is returned as a Bitmap handle.
     
    For frame capture to BMP or JPEG files, the file name generated automatically is also returned by this event.
     
    You can get various frame infos from this event by using GetFrameInfo (FrameId, ...).
     
    Note: the frame grabber must be enabled to capture video frames.
     
    Sample code is included in the MainDemo project.
     
    To get information about the current video frame, invoke GetFrameInfo (FrameId, ...). E.g.
    SampleTimeHour  = VideoGrabber.GetFrameInfo (FrameId, fi_SampleTime_Hour)
    

     

     


    參考   BC++  Sample
    Code Snippet
     
    1. //---------------------------------------------------------------------------    
    2. void __fastcall TForm1::VideoGrabber1FrameCaptureCompleted(TObject *Sender,   
  •       Pointer Bitmap, TFrameCaptureDest DestType, AnsiString FileName,   
  •       bool Success)   
  • //---------------------------------------------------------------------------    
  • {   
  •    TForm *DestForm;   
  •    TImage *DestImage;   
  •    Graphics::TBitmap *Captured_Bitmap; // redeclaration caused by the ambiguity between windows::Bitmap and Graphics::Bitmap    
  •                                        // to avoid the problem, Bitmap is passed as a simple pointer instead of a TBitmap.    
  •    
  •    Captured_Bitmap = (Graphics::TBitmap *) Bitmap;   
  •    
  •    // ------ bmp or jpeg file    
  •    if ((DestType == fc_BmpFile) || (DestType == fc_JpegFile)) {   
  •       if (Success) {   
  •           Memo1->Lines->Add (FileName + " successfully created.");   
  •        }   
  •        else {   
  •           Memo1->Lines->Add ("Unable to write " + FileName);   
  •        }   
  •    }   
  •    // ------ memory bitmap    
  •    else if (DestType == fc_TBitmap) {   
  •       if ((! VideoGrabber1->BurstMode) || (VideoGrabber1->BurstMode && (VideoGrabber1->BurstCount == 0)) ) { // reuse the same form    
  •          if (SnapForm == NULL) {   
  •             Application->CreateForm (__classid (TForm), &SnapForm);   
  •             SnapForm->Position = poDefault;   
  •             SnapForm->FormStyle = fsStayOnTop;   
  •          }   
  •          if (SnapImage != NULL) {   
  •             SnapImage->Free();   
  •          }   
  •          SnapImage = new TImage (NULL);   
  •          DestForm = SnapForm;   
  •          DestImage = SnapImage;   
  •       }   
  •       else { // create a form for each image    
  •          Application->CreateForm (__classid (TForm), &DestForm);   
  •          DestForm->Position = poDefault;   
  •          DestForm->FormStyle = fsStayOnTop;   
  •          DestImage = new TImage (DestForm);   
  •          DestImage->Parent = DestForm;   
  •       }   
  •    
  •       DestForm->ClientWidth = Captured_Bitmap->Width;   
  •       DestForm->ClientHeight = Captured_Bitmap->Height;   
  •       DestImage->AutoSize = true;   
  •       DestImage->Picture->Bitmap->Assign (Captured_Bitmap);   
  •       DestImage->Parent = DestForm;   
  •       if (! DestForm->Visible ) {   
  •          DestForm->Show();   
  •          Show();   
  •       }   
  •    }   
  • }   

     

    2008年11月15日 上午 06:10
  • VB 寫法 的 OnFrameCaptureCompleted 事件處理程序

    Code Snippet

     

    Private Sub VideoGrabber_OnFrameCaptureCompleted(ByVal sender As Object, ByVal e As Axvidgrab_NET.IVideoGrabberNETEvents_OnFrameCaptureCompletedEvent) Handles VideoGrabber.OnFrameCaptureCompleted
            Dim nDC As Integer
            Dim OldBitmap As Integer


            If e.success Then
                AddLog((FrameGrabber.DefInstance.mmoFrameCaptureLog), "frame #" & System.Convert.ToString(e.frameNumber) & " captured")
            End If



            Select Case e.destType
                '______ bmp or jpeg file
            Case vidgrab_NET.TxFrameCaptureDest.fc_JpegFile
                    If e.success Then
                        AddLog((FrameGrabber.DefInstance.mmoFrameCaptureLog), e.fileName)
                    Else
                        AddLog((FrameGrabber.DefInstance.mmoFrameCaptureLog), "error: failed to create " & e.fileName)
                    End If

                    '______ memory bitmap
                Case vidgrab_NET.TxFrameCaptureDest.fc_TBitmap
                    If Not SnapForm.DefInstance.Visible Then
                        SnapForm.DefInstance.Show()
                    End If
                    If Not SnapForm.DefInstance.SnapImage.Image Is Nothing Then
                        SnapForm.DefInstance.SnapImage.Image.Dispose()
                    End If
                    SnapForm.DefInstance.SnapImage.Image = Image.FromHbitmap(New IntPtr(e.frameBitmapHandle))
            End Select
        End Sub

     

    2008年11月15日 上午 06:20
  • Q:利用此指標將圖存在一陣列中或BitMap中 ?

     

    A:

    Code Snippet

     


        frmSnapForm.SnapImage.Image = Image.FromHbitmap((System.IntPtr)e.frameBitmapHandle);


            
            Bitmap bmp = frmSnapForm.SnapImage.Image ;

            // Lock the bitmap's bits. 
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bmp.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int bytes  = bmpData.Stride * bmp.Height;
         byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

            // Set every third value to 255. A 24bpp bitmap will look red. 
            for (int counter = 2; counter < rgbValues.Length; counter += 3)
                rgbValues[counter] = 255;

            // Copy the RGB values back to the bitmap
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

            // Unlock the bits.
            bmp.UnlockBits(bmpData);

              

     

     

     

     

    2008年11月15日 上午 07:06
  • 不好意思,再請教一下

        frmSnapForm.SnapImage.Image = Image.FromHbitmap((System.IntPtr)e.frameBitmapHandle);
            
            Bitmap bmp = frmSnapForm.SnapImage.Image ;

     

    這兩段程式中是否可以只接提取 e.frameBitmapHandle 至記憶體,而不是經過個frmSnapForm.SnapImage.Image表單再到記憶體中?

    2008年11月15日 上午 08:56
  •         
            Bitmap bmp =  Image.FromHbitmap((System.IntPtr)e.frameBitmapHandle);

    2008年11月15日 上午 08:59
  • 謝謝你的回覆,攫取圖的問題已解決了。再請教個問題One-shot frame capture與Burst-mode frame capture這兩種攫取差別是怎樣?

     

    2008年11月15日 下午 12:34
  • 還有個問題~~在執行                

     // Copy the RGB values into the array.
      System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

    後,他就自動離開這他所在函式,既使我在她後面幾行程式有設中斷點也無效?

     

    2008年11月15日 下午 12:53
  •  Alantis 寫信:
    謝謝你的回覆,攫取圖的問題已解決了。再請教個問題One-shot frame capture與Burst-mode frame capture這兩種攫取差別是怎樣?

     

     

    TVideoGrabber.BurstMode

     

    Enables/disables the automated frame capture process.
     

    Description

    Used to enable/disable the automated frame capture process.
    The number of frames captured depends of the BurstCount and BurstInterval parameters.
    The destination of the captured frame depends of the BurstType parameter.
    2008年11月17日 上午 03:13