Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all I have tried to build a SharePoint 2010 Visual Web Part that
enables a Flat-Bed Scanner and using that scan an image triggering from a
SharePoint Visual Web Part. The source code is as the below.

CreateFolderUserControl.ascx

C#
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Data;
using System.IO;
using System.Text;
using System.Linq;
using WIA;

namespace SPCreateFolder.CreateFolder
{
    public partial class CreateFolderUserControl : UserControl
    {
        private string strNewLibName;
        private string strNewFolName;
        private string strNewLibDesc;

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {

            strNewLibName = txtNewLibName.Text;
            strNewLibDesc = txtLibDesc.Text;
            strNewFolName = txtNewFolName.Text;

            SPSite mySite = new SPSite("Http://thekingsbury/");
            {
                SPWeb myWeb = mySite.OpenWeb();
                {
                    myWeb.Lists.Add(strNewLibName, strNewLibDesc, SPListTemplateType.DocumentLibrary);
                    myWeb.Update();

                    SPDocumentLibrary newDocLibrary = (SPDocumentLibrary)myWeb.Lists[strNewLibName];
                    SPFolderCollection newFolders = myWeb.Folders;
                    newFolders.Add("http://thekingsbury/" + strNewLibName + "/" + strNewFolName + "/");
                    newDocLibrary.Update();
                }
            }

            ClearAll();

        }

        private void ClearAll()
        {
            txtNewLibName.Text = "";
            txtLibDesc.Text = "";
            txtNewFolName.Text = "";
        }

        protected void btnScanTheDoc_Click(object sender, EventArgs e)
        {
            litScanner.Items.Clear();

            //Creating a device manager.
            var DeviceManager = new DeviceManager();

            // Loop through the list of devices and add the name to the listbox
            for (int i = 1; i <= DeviceManager.DeviceInfos.Count; i++)
            {
                var deviceName = DeviceManager.DeviceInfos[i].Properties["Name"].get_Value().ToString();
                litScanner.Items.Add(deviceName);
            }
            
            // Scanner selected?
            var device = litScanner.SelectedItem as Scanner;

            if (device == null)
            {
                //MessageBox.Show("Please select a device.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                lblDetScan.Enabled = true;
                return;
            }

            // Scan
           // var image = device.

            // Save the image
            var path = @"c:\scan.jpeg";


            if (File.Exists(path))
            {
                File.Delete(path);
            }
            image.SaveFile(path);
        }
    }
}


Scanner Class (Scanner.cs)

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using WIA;


namespace SPCreateFolder
{
    public class Scanner
    {
        private readonly DeviceInfo _deviceInfo;

        public Scanner(DeviceInfo deviceInfo)
        {
            this._deviceInfo = deviceInfo;
        }

        public Scanner()
        {
            // TODO: Complete member initialization
        }

        public ImageFile Scan()
        {
            // Connect to the device
            var device = this._deviceInfo.Connect();

            // Start the scan
            var item = device.Items[1];
            var imageFile = (ImageFile)item.Transfer(FormatID.wiaFormatJPEG);

            // Return the imageFile
            return imageFile;
        }
    }
}


But I've got an error at the CreateFolderUserControl.ascx file as the below.

C#
var device = litScanner.SelectedItem as Scanner;


The error is: "Cannot
convert type 'System.Web.UI.WebControls.ListItem' to
'SPCreateFolder.Scanner' via a reference conversion, boxing conversion,
unboxing conversion, wrapping conversion, or null type conversion
D:\Jobs\SPCreateFolder\SPCreateFolder\CreateFolder\CreateFolderUserControl.ascx.cs
72 26 SPCreateFolder "

Then how
am I supposed to solve that error? I use Visual Web Part in Visual
Studio 2010 for SharePoint 2010. Visual Web Part Gives me the ability to
do the same as in SharePoint Server with SharePoint Foundation. So
someone can help me to solve this matter. Please help me.

Thanks,

Regards,

Chiranthaka
Posted
Comments
Richard C Bishop 5-Nov-13 11:12am    
It means exactly what is says. You are trying to convert a list item to a scanner. If you are just needing to check if one is selected do this:

if(litScanner.SelectedItem != null)
{
}
Chiranthaka Sampath 4-Dec-13 23:33pm    
Could you please see the below comment and could you give me a solution for my errors?
Thanks
Chiranthaka
Chiranthaka Sampath 9-Dec-13 2:44am    
Why aren't anyone helping me? I am fed of this error. Could you someone solve this matter? Please.
Chiranthaka Sampath 21-Nov-13 1:30am    
I have the error in the following statement & the statement is used to pass a selected item as a scanner to a device.

var device = disScanners.SelectedItem as Scanner;

The error is saying that " Cannot convert type 'System.Web.UI.WebControls.ListItem' to 'SPCreateFolder.Scanner' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion. "

Then how am I supposed to solve this error. Because the previous comment you have given is working but how it will allow the scanned to be selected. For more information please refer the source code I have provided.
Richard C Bishop 9-Dec-13 10:24am    
Did you check the scanner documentation to see how to select one?

1 solution

I have found a solution & the solution is that when connecting hardware devices via ASP.Net or SharePoint we have to rely on 3rd party software application like Kodak Capture Pro and suchlike. Atlasoft also provies the same type of software tools with or without the SDK.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900