C# 保存位图图像

C# 保存位图图像,c#,winforms,bitmap,save,C#,Winforms,Bitmap,Save,我正在尝试修改机器视觉库(Common vision Blox)的一个示例。该示例从相机获取图像,将其转换为位图格式,并通过PictureBox向用户显示 我想要的是将数据保存到一个文件中,但我得到一个全黑的BMP。我尝试了一些变体,但结果是一样的 如果我使用PictureBox.SaveImage从PictureBox保存图像,如果我 代码如下: private Bitmap _bm; private bool _bRequiresCopy; private void cvImg_Im

我正在尝试修改机器视觉库(Common vision Blox)的一个示例。该示例从相机获取图像,将其转换为位图格式,并通过PictureBox向用户显示

我想要的是将数据保存到一个文件中,但我得到一个全黑的BMP。我尝试了一些变体,但结果是一样的

如果我使用PictureBox.SaveImage从PictureBox保存图像,如果我

代码如下:

private Bitmap  _bm;
private bool    _bRequiresCopy;
private void cvImg_ImageUpdated(object sender, EventArgs e)
{
  // create a bitmap out of the image
  _bm = CvbImageToBitmap(cvImg.Image, out _bRequiresCopy);
  // and display it in the picture box

  //if (_bm != null) // Only this two lines in the original code 
  //    pictureBox.Image = _bm; 

  if (_bm != null)
  {          
      pictureBox.Image = _bm;

      // Tests to save

      _bm.Save("c:\\test.bmp"); // This don't work

      // The following don't work, but if add a button to the form and just place in the click 
      // event of the button it stores the image correctly
      pictureBox.Image.Save("c:\\test2.bmp"); 

      Bitmap TestBMP = new Bitmap(_bm);
      TestBMP.Save("c:\\test3.bmp"); // This don't work

  }
  // setup grab checkbox
  chkGrab.Enabled = cvImg.IsGrabber;
  chkGrab.Checked = false;
}

“不工作”不是一个有用的描述。你能看到图片盒里的图像吗?另外,请始终使用相关平台标记所有问题:WPF、ASP、Winforms!是的,我可以在图片盒里看到这张图片。当我试图保存它时,我无法得到相同的图像。如果我调用pictureBox.Image.Save(“c:\\test2.bmp”);从另一个功能,即从另一个按钮,它保存正确。听起来像一个奇怪的时间问题。尝试将保存放在Picturebox.LoadCompleted事件中!在pictureBox.Image=\u bm;写入pictureBox.refresh();谢谢你的帮助。我可以用两种方法保存它。