PictureBox 转位图还是图像?
我正在尝试更改代码 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
以及在主窗体上更改它:
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
I am trying to change the code http://sites.google.com/site/webcamlibrarydotnet/winfrom-and-csharp-sample-code-and-download from picture box to image or bitmap as I dont want to display any image or plan to display, all I want is that it will output the image to file.
I have tried changing the webcam.cs from PictureBox _FrameImage
to Bitmap _FrameImage
and PictureBox ImageControl
to Bitmap ImageControl
and casting (Bitmap) at e.WebCamImage
As well as changing it on the main form:
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();
}
- Unhappyly it doesnt seem to work so
how could I change it from picture
box to Bitmap or Image or similar
storage before saving it to a file or
save it to file directly ?
The source code I am using is:
http://sites.google.com/site/webcamlibrarydotnet/winfrom-and-csharp-sample-code-and-download
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与其使用 WebCam 类,为什么不直接使用
WebCamCapture
类(因为您没有在表单中显示它)并直接处理ImageCapture
事件。该事件的事件参数包含Image
。您可以在事件处理程序中将图像保存到磁盘。或者,如果您想使用示例和WebCam
类,并且您有一个表单。使用 PictureBox 但将其隐藏(将 Visible 设置为 false),然后只需从那里复制图像并在需要时保存到磁盘。以下是使用 WebCamCapture 类而不是 WebCam 类的一些示例代码。应该注意的是,此代码基于问题中提供的链接中的示例代码。我保留了示例的风格,以便代码保持一致。
编辑:添加使用 WebCamCapture 而不是 WebCam 类的示例。此代码应用于修改示例代码中的
Form1.cs
。Instead of using the WebCam class, why not just use the
WebCamCapture
class directly (since you are not displaying this in a form) and handle theImageCapture
event directly. The event argument for the event contains theImage
. You could, in the event handler save the image to disk. Alternately, if you want to use the sample and theWebCam
class, and you have a form. Use a PictureBox but leave it hidden (set Visible to false) and then just copy the image from there and save to disk when you need to.Here is some sample code of using the WebCamCapture class instead of the WebCam class. It should be noted that this code is based on the sample code from the link provided in the question. I have kept the style of the sample so that code lines up.
Edit: Adding example of using WebCamCapture instead of WebCam class. This code should be used to modify
Form1.cs
in the sample code.使用反射器,您可以看到 WebCam 类在内部使用计时器来模拟帧速率。因此,紧随其后调用启动和停止永远不会生成图像,因为应用程序在启动和停止之间不处理应用程序事件(因此也不处理计时器滴答事件)。您应该注册 ImageChanged 事件并在那里调用 stop 。
祝你好运
** 编辑:启动逻辑 **
Using reflector you can see that internally the WebCam class uses a timer to simulate a framerate. Therefore calling start and stop right after each other will never generate an image since the application doesnt handle application events (and therefore the timer tick event) in between starting and stopping. You should register on the ImageChanged event and call stop in there.
Good luck
** Edit: the start logic **
在 WebCam.cs 中,您有:
如果修改 ImageCaptured 代码,您可以执行您想要的操作:e.WebCamImage 是一个图像。
例如,您可以更改/添加构造函数以接受文件名,并且在 ImageCaptured 事件中,您可以将图像保存到文件。
In WebCam.cs you have:
If you modify the ImageCaptured code you can do what you want: e.WebCamImage is an Image.
for example you could change/add constructor to accept a file name and, in the ImageCaptured event, you could save image to file.