PictureBox 转位图还是图像?

发布于 2024-10-31 05:15:25 字数 1234 浏览 1 评论 0原文

我正在尝试更改代码 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷你仙 2024-11-07 05:15:25

与其使用 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);
}

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 the ImageCapture event directly. The event argument for the event contains the Image. You could, in the event handler save the image to disk. Alternately, if you want to use the sample and the WebCam 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.

// 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);
}
梦归所梦 2024-11-07 05:15:25

使用反射器,您可以看到 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();
    }
}

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 **

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();
    }
}
漫漫岁月 2024-11-07 05:15:25

在 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 事件中,您可以将图像保存到文件。

In WebCam.cs you have:

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;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文