C#获取尼康D5100里拍好的照片

简介:

一台尼康D5100数码相机,里面已经拍好了若干照片,通过USB连接电脑。现在通过C#程序,获取其相片集合。

主要通过WIA操作。WIA:Microsoft Windows Image Acquisition Library v2.0。

这是一个COM操作,需要在项目里右键引用,选择COM,勾选Microsoft Windows Image Acquisition Library v2.0。

引用成功后,右键此WIA->属性,将“嵌入互操作类型”修改为False。

 

WInform代码如下(界面上添加一个Button按钮,一个PictureBox即可):

  private void button1_Click(object sender, EventArgs e)
        {
            Device device = null;

            DeviceManager deviceManager = new DeviceManagerClass();
            foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos)
                if (deviceInfo.Type == WiaDeviceType.CameraDeviceType)
                {
                    device = deviceInfo.Connect();
                    break;
                }


            List<Image> imgList = new List<Image>();

            int count = device.Items.Count;
            for(int i=1;i<=count;i++)
            {
                ImageFile imageFile = (ImageFile)device.Items[i].Transfer(FormatID.wiaFormatTIFF);
                imgList.Add( ExtractImages(imageFile).FirstOrDefault());
            }

            this.pictureBox1.Image = imgList[0]; 
        } 


        /// <summary>
        /// Liest alle Bilder aus dem angegebenen ImageFile-Objekt aus 
        /// </summary>
        /// <param name="imageFile">das ImageFile-Objekt, dessen enthaltene Bilder ausgelesen werden sollen</param>
        /// <returns>eine Auflistung der Bilder, die das ImageFile-Objekt enthält</returns>
        private static IEnumerable<Image> ExtractImages(ImageFile imageFile)
        {
            for (int frame = 1; frame <= imageFile.FrameCount; frame++)
            {
                imageFile.ActiveFrame = frame;
                ImageFile argbImage = imageFile.ARGBData.get_ImageFile(imageFile.Width, imageFile.Height);

                Image result = Image.FromStream(new MemoryStream((byte[])argbImage.FileData.get_BinaryData()));
                yield return result;
            }
        }

 

备注:附带资源下载资源

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值