Let's Start with step by step instructions to create the WPF browser application for scanning the documents.
Step 1: First open the visual studio and select the project type WPF Browser based Application.
Check in this Image how to create WPF browser application,

Step 2: In the second step design your page as you want to view in browser accordingly.
Step 3: Now you have to add the Interop.WIA.dll and itextsharp.dll in your project reference.
Step 4: Now you have to add System.Drawing and System.Web Assemblies.
Step 5: Now Generate the click event to start the scanning.

Step 6:
Here I have given all the code, just go through that and you can change the code according to your requirement or just copy and paste into your cs page.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using WIA;
  16. using System.Deployment.Application;
  17. using System.Runtime.InteropServices;
  18. using System.Windows.Interop;
  19. using System.Collections.Specialized;
  20. using System.Web;
  21. using System.Security.Cryptography;
  22. using System.Reflection;
  23. using System.Configuration;
  24. using System.IO;
  25. using System.Drawing.Imaging;
  26. namespace WpfBrowserApplication1
  27. {
  28. /// <summary>
  29. /// Interaction logic for Page1.xaml
  30. /// </summary>
  31. public partial class Page1 : Page
  32. {
  33. List<System.Drawing.Image> obj = new List<System.Drawing.Image>();
  34. List<Scannerdata> objListSD = new List<Scannerdata>();
  35. int GlobalSquence = 1;
  36. int count = 0;
  37. int SequenceTotal = 1;
  38. public Page1()
  39. {
  40. InitializeComponent();
  41. _deviceId = GetDefaultDeviceID();
  42. _deviceInfo = FindDevice(_deviceId);
  43. _device = _deviceInfo.Connect();
  44. btnPreview.IsEnabled = false;
  45. btnNext.IsEnabled = false;
  46. }
  47. #region WIA constants
  48. public const int WIA_DPS_DOCUMENT_HANDLING_CAPABILITIES = 3086;
  49. public const int WIA_DPS_DOCUMENT_HANDLING_STATUS = 3087;
  50. public const int WIA_DPS_DOCUMENT_HANDLING_SELECT = 3088;
  51. public const string WIA_FORMAT_JPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
  52. public const int FEED_READY = 0x00000001;
  53. public const int FLATBED_READY = 0x00000002;
  54. public const uint BASE_VAL_WIA_ERROR = 0x80210000;
  55. public const uint WIA_ERROR_PAPER_EMPTY = BASE_VAL_WIA_ERROR + 3;
  56. #endregion
  57. private string _deviceId;
  58. private DeviceInfo _deviceInfo;
  59. private Device _device;
  60. private DeviceInfo FindDevice(string deviceId)
  61. {
  62. DeviceManager manager = new DeviceManager();
  63. foreach (DeviceInfo info in manager.DeviceInfos)
  64. if (info.DeviceID == deviceId)
  65. return info;
  66. return null;
  67. }
  68. private string GetDefaultDeviceID()
  69. {
  70. string deviceId = string.Empty;
  71. //string deviceId = Properties.Settings.Default.ScannerDeviceID;
  72. //if (String.IsNullOrEmpty(deviceId))
  73. //{
  74. Device d = null;
  75. WIA.CommonDialog wiaDiag = new WIA.CommonDialog();
  76. try
  77. {
  78. d = wiaDiag.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
  79. if (d != null)
  80. {
  81. deviceId = d.DeviceID;
  82. Properties.Settings.Default.ScannerDeviceID = deviceId;
  83. Properties.Settings.Default.Save();
  84. }
  85. else
  86. {
  87. MessageBox.Show("Check the Device Connection \n or \n Change the Scanner Device", "Device Not Found!", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation);
  88. var hostScript = BrowserInteropHelper.HostScript;
  89. hostScript.document.ResponseData("");
  90. }
  91. }
  92. catch (Exception err)
  93. {
  94. MessageBox.Show("Scaner device not found in your system", "Devic Not Found!", MessageBoxButton.OKCancel, MessageBoxImage.Error);
  95. var hostScript = BrowserInteropHelper.HostScript;
  96. hostScript.document.ResponseData("");
  97. }
  98. return deviceId;
  99. }
  100. public List<System.Drawing.Image> ScanPages(int dpi = 150, double width = 8.27, double height = 11.69)
  101. {
  102. Item item = _device.Items[1];
  103. // configure item
  104. SetDeviceItemProperty(ref item, 6146, 2); // greyscale
  105. SetDeviceItemProperty(ref item, 6147, dpi); // 150 dpi
  106. SetDeviceItemProperty(ref item, 6148, dpi); // 150 dpi
  107. SetDeviceItemProperty(ref item, 6151, (int)(dpi * width)); // scan width
  108. SetDeviceItemProperty(ref item, 6152, (int)(dpi * height)); // scan height
  109. SetDeviceItemProperty(ref item, 4104, 8); // bit depth
  110. // TODO: Detect if the ADF is loaded, if not use the flatbed
  111. List<System.Drawing.Image> images = GetPagesFromScanner(ScanSource.DocumentFeeder, item);
  112. if (images.Count == 0)
  113. {
  114. // Try from flatbed
  115. // DialogResult dialogResult;
  116. MessageBoxResult dialogResult;
  117. do
  118. {
  119. List<System.Drawing.Image> singlePage = GetPagesFromScanner(ScanSource.Flatbed, item);
  120. images.AddRange(singlePage);
  121. dialogResult = MessageBox.Show("Do you want to scan another page?", "ScanToEvernote", MessageBoxButton.YesNo, MessageBoxImage.Question);
  122. }
  123. while (dialogResult == MessageBoxResult.Yes);
  124. }
  125. return images;
  126. }
  127. private List<System.Drawing.Image> GetPagesFromScanner(ScanSource source, Item item)
  128. {
  129. SetDeviceProperty(ref _device, 3088, (int)source);
  130. List<System.Drawing.Image> images = new List<System.Drawing.Image>();
  131. int handlingStatus = GetDeviceProperty(ref _device, WIA_DPS_DOCUMENT_HANDLING_STATUS);
  132. if ((source == ScanSource.DocumentFeeder && handlingStatus == FEED_READY) || (source == ScanSource.Flatbed && handlingStatus == FLATBED_READY))
  133. {
  134. do
  135. {
  136. ImageFile wiaImage = null;
  137. try
  138. {
  139. wiaImage = item.Transfer(WIA_FORMAT_JPEG);
  140. }
  141. catch (COMException ex)
  142. {
  143. if ((uint)ex.ErrorCode == WIA_ERROR_PAPER_EMPTY)
  144. break;
  145. else
  146. throw;
  147. }
  148. if (wiaImage != null)
  149. {
  150. System.Diagnostics.Trace.WriteLine(String.Format("Image is {0} x {1} pixels", (float)wiaImage.Width / 150, (float)wiaImage.Height / 150));
  151. System.Drawing.Image image = ConvertToImage(wiaImage);
  152. images.Add(image);
  153. }
  154. }
  155. while (source == ScanSource.DocumentFeeder);
  156. }
  157. return images;
  158. }
  159. private static System.Drawing.Image ConvertToImage(ImageFile wiaImage)
  160. {
  161. byte[] imageBytes = (byte[])wiaImage.FileData.get_BinaryData();
  162. MemoryStream ms = new MemoryStream(imageBytes);
  163. System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
  164. // Image image = Image.FromStream(ms);
  165. return image;
  166. }
  167. #region Get/set device properties
  168. private void SetDeviceProperty(ref Device device, int propertyID, int propertyValue)
  169. {
  170. foreach (Property p in device.Properties)
  171. {
  172. if (p.PropertyID == propertyID)
  173. {
  174. object value = propertyValue;
  175. p.set_Value(ref value);
  176. break;
  177. }
  178. }
  179. }
  180. private int GetDeviceProperty(ref Device device, int propertyID)
  181. {
  182. int ret = -1;
  183. foreach (Property p in device.Properties)
  184. {
  185. if (p.PropertyID == propertyID)
  186. {
  187. ret = (int)p.get_Value();
  188. break;
  189. }
  190. }
  191. return ret;
  192. }
  193. private void SetDeviceItemProperty(ref Item item, int propertyID, int propertyValue)
  194. {
  195. foreach (Property p in item.Properties)
  196. {
  197. if (p.PropertyID == propertyID)
  198. {
  199. object value = propertyValue;
  200. p.set_Value(ref value);
  201. break;
  202. }
  203. }
  204. }
  205. private int GetDeviceItemProperty(ref Item item, int propertyID)
  206. {
  207. int ret = -1;
  208. foreach (Property p in item.Properties)
  209. {
  210. if (p.PropertyID == propertyID)
  211. {
  212. ret = (int)p.get_Value();
  213. break;
  214. }
  215. }
  216. return ret;
  217. }
  218. #endregion
  219. private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value);
  220. /// <summary>
  221. /// Button_Click Used for start the sacnning
  222. /// </summary>
  223. /// <param name="sender"></param>
  224. /// <param name="e"></param>
  225. private void Button_Click(object sender, RoutedEventArgs e)
  226. {
  227. //Make butone enable false
  228. btnAttach.IsEnabled = false;
  229. btnScan.IsEnabled = false;
  230. pic_scan.Source = null;
  231. lbl_message.Content = string.Empty;
  232. //Configure the ProgressBar
  233. ProgressBar1.Minimum = 0;
  234. ProgressBar1.Maximum = 1000;
  235. ProgressBar1.Value = 0;
  236. //Stores the value of the ProgressBar
  237. double value = 0;
  238. //Create a new instance of our ProgressBar Delegate that points
  239. // to the ProgressBar's SetValue method.
  240. UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(ProgressBar1.SetValue);
  241. //Tight Loop: Loop until the ProgressBar.Value reaches the max
  242. do
  243. {
  244. value += 1;
  245. /*Update the Value of the ProgressBar:
  246. 1) Pass the "updatePbDelegate" delegate that points to the ProgressBar1.SetValue method
  247. 2) Set the DispatcherPriority to "Background"
  248. 3) Pass an Object() Array containing the property to update (ProgressBar.ValueProperty) and the new value */
  249. Dispatcher.Invoke(updatePbDelegate,
  250. System.Windows.Threading.DispatcherPriority.Background,
  251. new object[] { ProgressBar.ValueProperty, value });
  252. }
  253. while (ProgressBar1.Value != ProgressBar1.Maximum);
  254. if (ProgressBar1.Value == ProgressBar1.Maximum)
  255. {
  256. ProgressBar1.IsEnabled = true;
  257. obj = ScanPages();//start scan method
  258. foreach (System.Drawing.Image aa in obj)
  259. {
  260. Scannerdata objSD = new Scannerdata();
  261. objSD.Sequence = SequenceTotal;
  262. objSD.ImageObj = aa;
  263. objListSD.Add(objSD);
  264. SequenceTotal++;
  265. }
  266. Scannerdata first = (from row in objListSD where row.Sequence == GlobalSquence select row).FirstOrDefault();
  267. LoadImage(first.ImageObj);
  268. // Application.Current.Properties["Objliost"] = obj.ToList();
  269. }
  270. if (obj.Count == 1)
  271. {
  272. btnPreview.IsEnabled = false;
  273. btnNext.IsEnabled = false;
  274. lbl_message.Visibility = Visibility.Visible;
  275. lbl_message.Content = "Single page scaning Completed.";
  276. }
  277. else if (obj.Count > 1)
  278. {
  279. //btnPreview.IsEnabled = true;
  280. btnNext.IsEnabled = true;
  281. lbl_message.Visibility = Visibility.Visible;
  282. lbl_message.Content = "Multiple page scaning Completed.";
  283. }
  284. btnAttach.IsEnabled = true;
  285. btnScan.IsEnabled = true;
  286. ProgressBar1.Value = 0;
  287. }
  288. /// <summary>
  289. /// Use for Load the Image in Image Box
  290. /// </summary>
  291. /// <param name="loaddata"></param>
  292. /// <returns></returns>
  293. public byte[] LoadImage(System.Drawing.Image loaddata)
  294. {
  295. BitmapImage bi = new BitmapImage();
  296. bi.BeginInit();
  297. MemoryStream ms = new MemoryStream();
  298. // Save to a memory stream...
  299. loaddata.Save(ms, ImageFormat.Jpeg);
  300. // Rewind the stream...
  301. ms.Seek(0, SeekOrigin.Begin);
  302. // cache the image
  303. bi.CacheOption = BitmapCacheOption.OnLoad;
  304. // Tell the WPF image to use this stream...
  305. bi.StreamSource = ms;
  306. bi.EndInit();
  307. pic_scan.Source = bi;
  308. bi.Freeze();
  309. return ms.ToArray();
  310. }
  311. /// <summary>
  312. /// Button_Click_3 used for saved the scanned documents in pdf.
  313. /// </summary>
  314. private void Button_Click_3(object sender, RoutedEventArgs e)
  315. {
  316. using (var ms = new MemoryStream())
  317. {
  318. var document = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER.Rotate(), 0, 0, 0, 0);
  319. iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms).SetFullCompression();
  320. document.Open();
  321. foreach (System.Drawing.Image aa in obj)
  322. {
  323. MemoryStream msimage = new MemoryStream();
  324. aa.Save(msimage, ImageFormat.Jpeg);
  325. var image = iTextSharp.text.Image.GetInstance(msimage.ToArray());
  326. image.ScaleToFit(document.PageSize.Width, document.PageSize.Height);
  327. document.Add(image);
  328. }
  329. document.Close();
  330. //Path will come from App config
  331. string Path = ConfigurationManager.AppSettings["uploadfolderpath"].ToString();//confige path
  332. string filename = "C3kycDMS" + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ".pdf";
  333. //Save using drive name
  334. File.WriteAllBytes(Path + filename, ms.ToArray());
  335. byte[] test = ms.ToArray();
  336. MessageBox.Show("File Uploaded Successfully", "Success!", MessageBoxButton.OKCancel);
  337. pic_scan.Source = null;
  338. }
  339. }
  340. /// <summary>
  341. /// Used for Next button to see the images
  342. /// </summary>
  343. /// <param name="sender"></param>
  344. /// <param name="e"></param>
  345. private void Button_Click_2(object sender, RoutedEventArgs e)
  346. {
  347. btnPreview.IsEnabled = true;
  348. if (GlobalSquence < SequenceTotal)
  349. {
  350. GlobalSquence++;
  351. }
  352. Scannerdata first = (from row in objListSD where row.Sequence == GlobalSquence select row).FirstOrDefault();
  353. LoadImage(first.ImageObj);
  354. if (GlobalSquence == SequenceTotal - 1)
  355. {
  356. btnNext.IsEnabled = false;
  357. }
  358. }
  359. /// <summary>
  360. /// Used for Preview the page
  361. /// </summary>
  362. /// <param name="sender"></param>
  363. /// <param name="e"></param>
  364. private void Button_Click_1(object sender, RoutedEventArgs e)
  365. {
  366. btnNext.IsEnabled = true;
  367. if (GlobalSquence < SequenceTotal)
  368. {
  369. GlobalSquence--;
  370. }
  371. Scannerdata first = (from row in objListSD where row.Sequence == GlobalSquence select row).FirstOrDefault();
  372. LoadImage(first.ImageObj);
  373. if (GlobalSquence == 1)
  374. {
  375. btnPreview.IsEnabled = false;
  376. }
  377. }
  378. }
  379. enum ScanSource
  380. {
  381. DocumentFeeder = 1,
  382. Flatbed = 2,
  383. }
  384. }
  385. public class Scannerdata
  386. {
  387. public int Sequence { get; set; }
  388. public System.Drawing.Image ImageObj { get; set; }
  389. }
Step 7: This is important you have to set device Id as seen in the below Image.

Step 8: If you are running WPF browser application make sure your site is a fuly trusted site if it is not you have to make this a fully trusted site.

To make it a fully trusted site follow this steps.

Goto SoluationExplorer -> Rightclick -> goto Properties -> select Security option -> now select Radio button Full trusted application -> Save and run.
Check in this Image,

I think this will help you but if you have any doubts or queries you can reach me any time.
Note: Same solution you can find with WINDOWS FORM -- to check this click on below link and check in my blogs,