2011-04-08 142 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