Accessing a flatbed scanner using C#

Akala ko mahirap to.. di naman pala sus!

Ang kailangan mo lang ay magdownload nung WIA tapos iregister sa Machine mo then reference mo sya sa project. tapos.. maglagay ka ng button then ganito ang code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WIA;
using System.IO;
using System.Runtime.InteropServices;
namespace scanner
    public partial class Form1 : Form
        string DeviceID;
        public Form1()
        private void Form1_Load(object sender, EventArgs e)
        void GoScan()
                //Choose Scanner
                CommonDialogClass cdcMine = new CommonDialogClass();
                Device d = cdcMine.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false);
                if (d != null)
                    //get the devide ID
                    this.DeviceID = d.DeviceID;
                    //no scanner chosen
                //create another common dialog to connect to the scanner
                CommonDialogClass WiaCommonDialog = new CommonDialogClass();
                //Create DeviceManager
                DeviceManager manager = new DeviceManagerClass();
                Device WiaDev = null;
                //get the info of all devices, look for the scanner
                foreach (DeviceInfo info in manager.DeviceInfos)
                    if (info.DeviceID == this.DeviceID)
                        WIA.Properties infoprop = null;
                        infoprop = info.Properties;
                        //connect to scanner
                        WiaDev = info.Connect();
                        //not very good coding practice. exit the IF when connected
                //Start Scan
                //create an image file
                WIA.ImageFile img = null;
                WIA.Item Item = WiaDev.Items[1] as WIA.Item;
                    //this should create an image file in memory
                    img = (ImageFile)WiaCommonDialog.ShowTransfer(Item, "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}", false);
                    //process image:
                    //one would do image processing here
                    //Save to file
                    SaveFileDialog sfd = new SaveFileDialog();
                    // Default file extension
                    sfd.DefaultExt = "jpg";
                    // Available file extensions
                    sfd.Filter = "jpeg file (*.jpg)|*.jpg";
                    // Adds a extension if the user does not
                    sfd.AddExtension = true;
                    // Restores the selected directory, next time
                    sfd.RestoreDirectory = true;
                    // Dialog title
                    sfd.Title = "Where do you want to save the file?";
                    // Startup directory
                    sfd.InitialDirectory = @"C:/";
                    if (sfd.ShowDialog() == DialogResult.OK)
                    string varImageFileName = sfd.FileName;
                    if (File.Exists(varImageFileName))
                        //file exists, delete it. or put up a more elegant error/naming handling scheme
                    img = null;
                    MessageBox.Show("Image succesfully scanned and saved as! " + Environment.NewLine + sfd.FileName + " HAH!");
                catch (Exception ex)
                    MessageBox.Show("Error: " + ex.Message);
                    Item = null;
            catch (Exception ee)
                MessageBox.Show("Error: " + ee.Message);
        private void button1_Click(object sender, EventArgs e)
yan.. pwede na yan! 🙂
huwag kalimutan ito ha
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s