【问题标题】:PictureBox to Bitmap or Image?PictureBox 到位图或图像?
【发布时间】:2011-08-01 13:10:07
【问题描述】:

我正在尝试将代码 http://sites.google.com/site/webcamlibrarydotnet/winfrom-and-csharp-sample-code-and-download 从图片框更改为图像或位图,因为我不想显示任何图像或计划显示,我想要的只是它将图像输出到文件。

我尝试将 webcam.cs 从PictureBox _FrameImage 更改为Bitmap _FrameImagePictureBox 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

【问题讨论】:

  • 如果你搜索 SO,Bitmap to Image 问题之前已经被问过
  • 为什么你不只调用 image.Save("testWebcam.jpg") ?
  • @Tony 它不是 Bitmap to Image 它是 PictureBox 到 2 之一。
  • @Anton 问题是图像始终为空,因此我对源代码所做的更改不起作用,这就是我寻求帮助的原因;)

标签: c# image picturebox


【解决方案1】:

为什么不直接使用WebCamCapture 类而不是使用WebCam 类(因为您没有在表单中显示它)并直接处理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);
}

【讨论】:

  • 也许你可以给我举个例子?我正在改变课程,因为我不知道如何进行捕获。
  • 我添加了一个示例 - 另一件需要注意的事情,并已在其他一些 cmets 中指出。在您收到图像捕获事件之前,请勿停止相机。从相机复制图像后,您可以使捕获事件处理程序停止相机。我将添加一个编辑来显示这一点。
  • 非常感谢您抽出宝贵的时间,它非常符合我的需求,感谢您抽出宝贵的时间。
【解决方案2】:

使用反射器可以看到,WebCam 类在内部使用计时器来模拟帧速率。因此,在彼此之后立即调用 start 和 stop 将永远不会生成图像,因为应用程序在启动和停止之间不处理应用程序事件(以及因此计时器滴答事件)。您应该注册 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();
    }
}

【讨论】:

  • 即使我将停止更改为事件,或者即使我不停止,它似乎也不起作用,它只是运行到保存并抛出空异常。
  • 你如何进行演员阵容?尝试使用 as 运算符并在保存(和停止)Bitmap bitmap = e.WebCamImage as Bitmap 之前检查 null; if (bitmap != null) { ... }
  • 不确定,但不能保证事件每次都会返回一个有效的图像,例如:第一个事件可能带有一个对 PictureBox 完全有效的空值
  • 很遗憾,我无法解决这个问题,无论我尝试什么,它总是为空。
【解决方案3】:

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

【讨论】:

  • 就像我在上面的问题中所说的那样,我确实更改了它们但不起作用;(
猜你喜欢
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 2017-07-08
  • 2016-05-18
  • 1970-01-01
  • 2011-05-03
  • 2018-03-17
  • 2013-05-25
相关资源
最近更新 更多