【问题标题】:How to get the last taken picture如何获取最后一张照片
【发布时间】:2012-02-08 19:45:13
【问题描述】:

有人能告诉我如何使用 WIA 从我的相机中获取最后一张照片的预览图像吗?

这就是您拍照所需的全部内容:

//select device
WIA.CommonDialog dialog = new WIA.CommonDialog();
WIA.Device camera = dialog.ShowSelectDevice(WIA.WiaDeviceType.CameraDeviceType, false, true);

//take picture 
camera.ExecuteCommand("{AF933CAC-ACAD-11D2-A093-00C04F72DC3C}");

这样我可以获取所有相机属性,但是没有最后一张图片信息:

string p = "";
foreach (Property p in camera.Properties)
{
    p += p.Name + ":\t" + p.get_Value() + "\n";
}
MessageBox.Show(p);

【问题讨论】:

  • 请不要在标题前加上“C# WIA:”之类的前缀。这就是标签的用途。
  • 请显示一些源代码...您尝试了什么?什么不工作?
  • 请注意,并非所有摄像头都支持 WIA。除非您针对特定设备,否则可能需要查看 DirectShow。
  • 我成功选择了我的相机(nikon d3000),我可以用它拍照,但现在我想预览这张照片...我没有代码,因为我没有想办法得到这张照片……

标签: c# winforms wia


【解决方案1】:

ExecuteCommand 返回一个 WIA.Item,它提供了一个 Transfer 方法:

WIA.CommonDialog dialog = new WIA.CommonDialog();
WIA.Device camera = dialog.ShowSelectDevice(WIA.WiaDeviceType.CameraDeviceType, false, true);
WIA.Item takenItem = camera.ExecuteCommand("{AF933CAC-ACAD-11D2-A093-00C04F72DC3C}");

foreach (string formatId in takenItem.Formats)
{
    if (Guid.Parse(formatId) == System.Drawing.Imaging.ImageFormat.Jpeg.Guid)
    {
        WIA.ImageFile wiaImage = takenItem.Transfer(formatId);

        var imageData = new MemoryStream( wiaImage.FileData.get_BinaryData());
        var image = Image.FromStream(imageData);
        //pictureBox1.Image = image;
    }
}

【讨论】:

  • 当我运行它时,我得到: System.NullReferenceException is unhandled Message=Object reference not set to an instance of an object.
  • 如果takenItem 为空,那么您可能选择了错误的设备,或者您的设备不支持此命令。您可以通过camera.Commands 查看您的设备支持的命令。否则调用Transfer 没有任何formatId
  • camera.Comands 只返回 2 个命令:Synchronize 和 TakePicture。我尝试调用 Transfer(),但错误仍然存​​在。
  • 我会接受你的回答,如果你的代码在其他相机上工作,我会告诉你……我的相机似乎不支持这个:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 2013-01-14
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多