[英]PictureBox to Bitmap or Image?
我正嘗試將代碼http://sites.google.com/site/webcamlibrarydotnet/winfrom-and-csharp-sample-code-and-download從圖片框更改為圖像或位圖,因為我不想顯示任何圖像或計划顯示,我只需要將圖像輸出到文件即可。
我嘗試將webcam.cs從PictureBox _FrameImage
為Bitmap _FrameImage
並將PictureBox ImageControl
更改為Bitmap ImageControl
並在e.WebCamImage
投射(Bitmap)
以及在主要形式上進行更改:
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
除了使用WebCam類之外,為什么不直接使用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);
}
使用反射器,您可以看到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();
}
}
在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事件中,可以將圖像保存到文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.