Windows Image Acquisition

出自ProgWiki
跳至導覽 跳至搜尋

Windows Image Acquisition(WIA),參照:『維基百科~Windows_Image_Acquisition

  • WIA作為影像掃描器、網路攝影機(WebCam)、數位攝影機、數位照相機……等設備的使用界面。

版本

版本 引用參照DLL 描述
1.0 C:\WINDOWS\system32\wiascr.dll Microsoft Windows Image Acquisition 1.01 Type Library
2.0 C:\WINDOWS\system32\wiaaut.dll Microsoft Windows Image Acquisition Library v2.0


技術文件

相關下載

SDK
應用範例

FAQ

在WIA 1.0裡無法將型別 'System.IntPtr' 轉換為 'WIAVIDEOLib._RemotableHandle'

public WIAVIDEOLib._RemotableHandle getRemotableHandle(IntPtr windowHandle)
{
	//Allocate unmanaged memory to store this IntPtr
	IntPtr address = Marshal.AllocHGlobal(IntPtr.Size);
 
	//Write the IntPtr into unmanaged memory
	Marshal.WriteIntPtr(address, windowHandle);
 
	//Return the RemotableHandle by marshalling it from unmanaged memory
	return Marshal.PtrToStructure(address, typeof(WIAVIDEOLib._RemotableHandle));
}

在WIA 2.0與WebCam應用範例

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using WIA;
using Microsoft.Win32;
 
namespace WebCamViewer
{
    class WebCam
    {
        public WebCam()
        {
            TempImagesDirectory = Path.GetTempPath();
            TempImagesIndex = 1;
 
            RegistryKey jpegKey = Registry.ClassesRoot.OpenSubKey(@"CLSID\{D2923B86-15F1-46FF-A19A-DE825F919576}\SupportedExtension\.jpg");
            jpegGuid = jpegKey.GetValue("FormatGUID") as string;
        }
 
        ~WebCam() { }
 
        public enum WIA_PpropertyID {
            WIA_DIP_DEV_ID = 2,
            WIA_DIP_VEND_DESC =3,
            WIA_DIP_DEV_DESC = 4,
            WIA_DIP_DEV_TYPE = 5,
            WIA_DIP_PORT_NAME = 6,
            WIA_DIP_DEV_NAME = 7,
            WIA_DIP_SERVER_NAME =8,
            WIA_DIP_REMOTE_DEV_ID =9,
            WIA_DIP_UI_CLSID = 10,
            WIA_DIP_HW_CONFIG = 11,
            WIA_DIP_BAUDRATE = 12,
            WIA_DIP_STI_GEN_CAPABILITIES = 13,
            WIA_DIP_WIA_VERSION = 14,
            WIA_DIP_DRIVER_VERSION = 15
        };
 
        private string TempImagesDirectory = null;
        private int TempImagesIndex;
        private PictureBox pictureBox1 = null;
 
        private Device device = null;
        private string DefaultDeviceId = null;
        private string DefaultDeviceDesc = null;
 
        private string jpegGuid = string.Empty;
 
 
        public void OpenDefaultDevice()
        {
            DeviceManager manager = new DeviceManagerClass();
            if (manager.DeviceInfos.Count == 1)
            {
                //單一裝置
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    device = info.Connect();
                    DefaultDeviceId = info.DeviceID;
                    object PpropertyID = WIA_PpropertyID.WIA_DIP_DEV_TYPE;
                    DefaultDeviceDesc = Convert.ToString(info.Properties.get_Item(ref PpropertyID).get_Value());
                }
            }
            else
            {
                //多重裝置(取第1個Vidio)
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    foreach (Property prop in info.Properties)
                    {
                        if (prop.PropertyID == (int)WIA_PpropertyID.WIA_DIP_DEV_TYPE)
                        {
                            Int32 propValue = (Convert.ToInt32(prop.get_Value()) >> 16);
                            if (propValue == (Int32)WIA.WiaDeviceType.VideoDeviceType)
                            {
                                device = info.Connect();
                                DefaultDeviceId = info.DeviceID;
                                object PpropertyID = WIA_PpropertyID.WIA_DIP_DEV_TYPE;
                                DefaultDeviceDesc = Convert.ToString(info.Properties.get_Item(ref PpropertyID).get_Value());
                                return;
                            }
                        }
                    }
 
                }
            }
        }
 
 
 
        public string TakePicture(bool IsOutToFile) {
            string filename = String.Empty;
            if (device != null)
            {
                Item item = device.ExecuteCommand(CommandID.wiaCommandTakePicture);
                foreach (string format in item.Formats)
                {
                    if (format == jpegGuid)
                    {
                        WIA.ImageFile imagefile = item.Transfer(format) as WIA.ImageFile;
 
                        if (pictureBox1 != null)
                        {
                            byte[] imageBytes = (byte[])imagefile.FileData.get_BinaryData();
                            MemoryStream ms = new MemoryStream(imageBytes);
                            Image img = Image.FromStream(ms);
                            pictureBox1.Image = img;
                        }
 
 
                        if (IsOutToFile) 
                        {
                            filename = GetTempImageFileName();
                            if (string.IsNullOrEmpty(filename) == false)
                            {
                                imagefile.SaveFile(filename);
                            }
                        }
 
                        return filename;
                    }
                }
            }
 
            return filename;
        }
 
 
        private string GetTempImageFileName()
        {
            string fileName = string.Format(
                "{0}{1}.jpg",
                TempImagesDirectory,
                TempImagesIndex);
 
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
 
            TempImagesIndex++;
 
            return fileName;
        }
 
        public void SetPictureBox(PictureBox pictureBox)
        {
            if (pictureBox != null)
                this.pictureBox1 = pictureBox;
        }
    }
}