Search code examples
c#byteworkflow-foundationscanningwia

converting .__comobj WIA to byte[] in C# application


I have the following code in my C# application. I want it to capture a scanning document using WIA into a byte[] variable, but i couldn't convert the .__comobject into byte[]

private void btnScan_Click(object sender, EventArgs e)
        {
            try
            {
                var deviceManager = new DeviceManager();

                DeviceInfo AvailableScanner = null;

                for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) // Loop Through the get List Of Devices.
                {
                    if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) // Skip device If it is not a scanner
                    {
                        continue;
                    }

                    AvailableScanner = deviceManager.DeviceInfos[i];

                    break;
                }

                var device = AvailableScanner.Connect(); //Connect to the available scanner.
                var ScanerItem = device.Items[1]; // select the scanner.
                var imgFile = ScanerItem.Transfer(FormatID.wiaFormatJPEG); //Retrive an image in Jpg format and store it into a variable.


                TypeConverter obj = TypeDescriptor.GetConverter(imgFile.GetType());
                byte[] bt = (byte[])obj.ConvertTo(imgFile, typeof(byte[]));
                MemoryStream ms = new MemoryStream(bt);
                pictureBox1.Image = Image.FromStream(ms);

                //PictureBox1.Image = img;
            }
            catch(COMException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

Solution

  • this is it just use WIA.ImageFile then convert it using FileData.get_BinaryData():

    WIA.ImageFile imagefile = item.Transfer(format) as WIA.ImageFile;
    byte[] imageBytes = (byte[])imagefile.FileData.get_BinaryData();