2011-04-08 110 views
1

我試圖將代碼http://sites.google.com/site/webcamlibrarydotnet/winfrom-and-csharp-sample-code-and-download從圖片框更改爲圖片或位圖,因爲我不想顯示任何圖像或計劃來顯示,我只想要將圖像輸出到文件。PictureBox到位圖或圖像?

我試圖在e.WebCamImage

改變從PictureBox _FrameImage的webcam.cs到Bitmap _FrameImagePictureBox ImageControlBitmap ImageControl和鑄造(位圖)以及改變它的主要形式:

private void bWebcam_Click(object sender, EventArgs e) 
{ 
    WebCam webcam = new WebCam(); 
    Bitmap image = null; 
    webcam.InitializeWebCam(ref image); 

    webcam.Start(); 
    webcam.Stop(); 

    FileStream fstream = new FileStream("testWebcam.jpg", FileMode.Create); 
    image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg); 
    fstream.Close(); 
} 
  • 不高興它似乎不工作,所以 我怎麼能將它從圖片 框更改爲位圖或圖像或類似 保存到一個文件或 保存到文件直接?

我使用的源代碼是: http://sites.google.com/site/webcamlibrarydotnet/winfrom-and-csharp-sample-code-and-download

+0

如果您搜索SO,則先前先詢問過Bitmap to Image問題 – 2011-04-08 14:01:25

+0

爲什麼不調用image.Save(「testWebcam.jpg」)? – 2011-04-08 14:01:55

+0

@Tony它不是位圖的圖像它是PictureBox的任何一個2. – Guapo 2011-04-08 14:03:17

回答

2

而是使用網絡攝像頭類的,爲什麼不直接使用WebCamCapture類(因爲你不是在顯示此表單)並直接處理ImageCapture事件。該事件的事件參數包含Image。您可以在事件處理程序中將圖像保存到磁盤。或者,如果您想使用樣本和WebCam類,並且您有表單。使用PictureBox,但將其隱藏(將Visible設置爲false),然後從該處複製圖像並在需要時保存到磁盤。

以下是使用WebCamCapture類而不是WebCam類的一些示例代碼。應該注意的是,該代碼基於問題中提供的鏈接的示例代碼。我保留了樣本的樣式,以便代碼排列起來。

編輯:添加使用WebCamCapture而不是WebCam類的示例。此代碼應該用於修改示例代碼中的Form1.cs

// Instead of having WebCam as member variable, have WemCamCapture 
WebCamCapture webCam; 

// Change the mainWinForm_Load function 
private void mainWinForm_Load(object sender, EventArgs e) 
{ 
    webCam = new WebCamCapture(); 
    webCam.FrameNumber = ((ulong)(0ul)); 
    webCam.TimeToCapture_milliseconds = 30; 
    webCam.ImageCaptured += webcam_ImageCaptured; 
} 

// Add the webcam Image Captured handler to the main form 
private void webcam_ImageCaptured(object source, WebcamEventArgs e) 
{ 
    Image imageCaptured = e.WebCamImage; 
    // You can now stop the camera if you only want 1 image 
    // webCam.Stop(); 
    // Add code here to save image to disk 
} 

// Adjust the code in bntStart_Click 
// (yes I know there is a type there, but to make code lineup I am not fixing it) 
private void bntStart_Click(object sender, Event Args e) 
{ 
    webCam.Start(0); 
} 
+0

也許你可以給我一個例子嗎?我正在改變班級,因爲我不知道如何執行抓取。 – Guapo 2011-04-08 14:06:49

+1

我已經添加了一個示例 - 另一個要注意的事項,並在其他一些評論中指出。在收到圖像捕捉事件之前,請勿停止相機。從攝像機複製圖像後,可以使捕捉事件處理程序停止攝像機。我將添加一個編輯來顯示。 – pstrjds 2011-04-08 14:48:10

+0

非常感謝我的讚賞,它對我所需要的東西非常有用,感謝您抽出時間。 – Guapo 2011-04-08 15:15:51

0

在WebCam.cs您有:

public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl) 
{ 
    webcam = new WebCamCapture(); 
    webcam.FrameNumber = ((ulong)(0ul)); 
    webcam.TimeToCapture_milliseconds = FrameNumber; 
    webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured); 
    _FrameImage = ImageControl; 
} 
void webcam_ImageCaptured(object source, WebcamEventArgs e) 
{ 
    _FrameImage.Image = e.WebCamImage; 
} 

如果修改ImageCaptured代碼,你可以做你想做的: e.WebCamImage是一個圖像。
例如,您可以更改/添加構造函數以接受文件名,並且在ImageCaptured事件中,可以將圖像保存到文件。

+0

就像我上面提到的問題,我確實改變了他們,但沒有工作;( – Guapo 2011-04-08 14:06:03

1

使用反射器,您可以看到內部WebCam類使用計時器模擬幀率。因此,在彼此之後立即調用開始和停止將永遠不會生成圖像,因爲應用程序在開始和停止之間不處理應用程序事件(因此計時器滴答事件)。你應該在ImageChanged事件上註冊並且在那裏調用stop。

好運

**編輯:啓動邏輯**

public void Start(ulong FrameNum) 
{ 
    try 
    { 
     this.Stop(); 
     this.mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, this.m_Width, this.m_Height, base.Handle.ToInt32(), 0); 
     Application.DoEvents(); 
     SendMessage(this.mCapHwnd, 0x40a, 0, 0); 
     SendMessage(this.mCapHwnd, 0x432, 0, 0); 
     this.m_FrameNumber = FrameNum; 
     this.timer1.Interval = this.m_TimeToCapture_milliseconds; 
     this.bStopped = false; 
     this.timer1.Start(); 
    } 
    catch (Exception exception) 
    { 
     MessageBox.Show("An error ocurred while starting the video capture. Check that your webcamera is connected properly and turned on.\r\n\n" + exception.Message); 
     this.Stop(); 
    } 
} 
+0

,這使得很多感謝你。 – Guapo 2011-04-08 14:07:28

+0

似乎不工作,即使我改變停止進入事件,或者即使我不停止,它 – Guapo 2011-04-08 14:20:36

+0

如何執行強制轉換?嘗試使用as運算符並在保存(並停止)之前檢查null爲空 位圖位圖= e.WebCamImage位圖; if(bitmap != null){...} – Polity 2011-04-08 14:27:55