wia驅動掃描儀
.net wia驅動掃描儀
通過各種途徑,將當前比較流行的驅動掃描儀封裝成了一個簡單實用的class,調用掃描儀時,只需要重新創建個對象即可,代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using WIA;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
namespace WMSclient.Scanner
{//手動掃描
class Wia_ScanClass
{
private class WIA_DPS_DOCUMENT_HANDLING_SELECT
{
public const uint FEEDER = 1u;
public const uint FLATBED = 2u;
}
private class WIA_DPS_DOCUMENT_HANDLING_STATUS
{
public const uint FEED_READY = 1u;
}
private class WIA_PROPERTIES
{
public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024u;
public const uint WIA_DIP_FIRST = 2u;
public const uint WIA_DPA_FIRST = 1026u;
public const uint WIA_DPC_FIRST = 2050u;
public const uint WIA_DPS_FIRST = 3074u;
public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS = 3087u;
public const uint WIA_DPS_DOCUMENT_HANDLING_SELECT = 3088u;
}
private class WIA_ERRORS
{
public const uint BASE_VAL_WIA_ERROR = 2149646336u;
public const uint WIA_ERROR_PAPER_EMPTY = 2149646339u;
}
public class IMAGEFORAT//圖片格式
{
public const string wiaFormatBMP="{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
public const string wiaFormatPNG="{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
public const string wiaFormatGIF="{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}";
public const string wiaFormatJPEG="{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
public const string wiaFormatTIFF="{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}";
}
public class IMAGEATTRIBUTE
{
public const string wia_scan_color_mode = "6146"; //顏色模版
public const string wia_horizontal_scan_resolution_dpi = "6147"; //水平分辨率
public const string wia_vertical_scan_resolution_dpi = "6148";//垂直分辨率
public const string wia_horizontal_scan_start_pixel = "6149";//水平像素
public const string wia_vertical_scan_start_pixel = "6150";//垂直像素
public const string wia_horizontal_scan_size_pixels = "6151";
public const string wia_vertical_scan_size_pixels = "6152";
public const string wia_scan_brightness_percents = "6154";//亮度
public const string wia_scan_contrast_percents = "6155";//對比度
}
public enum ScanColor
{
Color = 1,
Gray,
BlackWhite = 4
}
/// <summary>
/// 掃描開始
/// flag=0,手動設置,此時attribute必須為null;
/// flag=1,默認設置,此時attribute不為空
/// </summary>
/// <param name="flag"></param>
/// <param name="attribute"></param>
/// <returns></returns>
public Dictionary<string, Image> BeginScan(int flag, Dictionary<string, int> attribute)
{
Dictionary<string, Image> pic = new Dictionary<string, Image>();
CommonDialogClass commonDialogClass = new CommonDialogClass();
//選擇掃描儀
Device device = getAndSelectDevice(commonDialogClass);
if (device != null)
{
string deviceID = device.DeviceID;
WIA.CommonDialog commonDialog = new WIA.CommonDialog();
DeviceManager deviceManager = new DeviceManagerClass();
Device device2 = null;
foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos)
{
if (deviceInfo.DeviceID == deviceID)
{
WIA.Properties properties = deviceInfo.Properties;
device2 = deviceInfo.Connect();
break;
}
}
try
{
Image image = null;
ImageFile imageFile = null;
Item item = null;
if (device2 != null)
{
switch (flag)
{
case 0: item = getHandleAdjustImageFormat(commonDialog, device2); break;
case 1: item = getAutoAdjustImageFormat(device2, attribute); break;
default: break;
}
//獲取圖片信息
imageFile = (ImageFile)getImageFile(commonDialog, IMAGEFORAT.wiaFormatJPEG, item);
if (imageFile!=null)
{
string fullpath = Path.GetTempFileName();
if (fullpath != null && !"".Equals(fullpath))
{
if (deleteFile(fullpath))
{
imageFile.SaveFile(fullpath);
image = Image.FromFile(fullpath);
if (image!=null)
{
pic.Add(fullpath, image);
}
else
{
MessageBox.Show("圖片轉換失敗", "提示");
return null;
}
}
else
{
MessageBox.Show("圖片刪除失敗", "提示");
return null;
}
}
else
{
MessageBox.Show("圖片路徑獲取失敗", "提示");
return null;
}
}
else
{
MessageBox.Show("掃描圖片獲取失敗", "提示");
return null;
}
}
else
{
MessageBox.Show("未連接到掃描儀", "提示");
return null;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "提示");
}
}
return pic;
}
/// <summary>
/// 刪除文件
/// </summary>
/// <returns></returns>
public bool deleteFile(string filePath)
{
Boolean flag = false;
if (File.Exists(filePath))
{
File.Delete(filePath);
flag = true;
}
return flag;
}
/// <summary>
/// 選擇掃描儀
/// </summary>
/// <returns></returns>
public Device getAndSelectDevice(CommonDialogClass commonDialogClass)
{
return commonDialogClass.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, false, false);
}
/// <summary>
/// 獲取imagefile
/// </summary>
/// <returns>ImageFile</returns>
///
public ImageFile getImageFile(WIA.CommonDialog commonDialog,string imageFrot, Item item)
{
return (ImageFile)commonDialog.ShowTransfer(item, IMAGEFORAT.wiaFormatJPEG, false);
}
private void setItem(IItem item, object property, object value)
{
WIA.Property aProperty = item.Properties.get_Item(ref property);
aProperty.set_Value(ref value);
}
/// <summary>
/// 自動調整掃描儀圖片格式:默認格式,並返回item
/// </summary>
/// <returns>Item</returns>
public Item getAutoAdjustImageFormat(Device device, Dictionary<string, int> attribute)
{
Item item = device.Items[1];
if (item!=null)
{
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_horizontal_scan_resolution_dpi))
setItem(item, IMAGEATTRIBUTE.wia_horizontal_scan_resolution_dpi, attribute[IMAGEATTRIBUTE.wia_horizontal_scan_resolution_dpi]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_horizontal_scan_size_pixels))
setItem(item, IMAGEATTRIBUTE.wia_horizontal_scan_size_pixels, attribute[IMAGEATTRIBUTE.wia_horizontal_scan_size_pixels]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_horizontal_scan_start_pixel))
setItem(item, IMAGEATTRIBUTE.wia_horizontal_scan_start_pixel, attribute[IMAGEATTRIBUTE.wia_horizontal_scan_start_pixel]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_scan_brightness_percents))
setItem(item, IMAGEATTRIBUTE.wia_scan_brightness_percents, attribute[IMAGEATTRIBUTE.wia_scan_brightness_percents]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_scan_color_mode))
{
if (attribute[IMAGEATTRIBUTE.wia_scan_color_mode]==1)
setItem(item, IMAGEATTRIBUTE.wia_scan_color_mode,ScanColor.Color);
if (attribute[IMAGEATTRIBUTE.wia_scan_color_mode] ==2)
setItem(item, IMAGEATTRIBUTE.wia_scan_color_mode, ScanColor.Gray);
if (attribute[IMAGEATTRIBUTE.wia_scan_color_mode] == 4)
setItem(item, IMAGEATTRIBUTE.wia_scan_color_mode, ScanColor.BlackWhite);
}
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_scan_contrast_percents))
setItem(item, IMAGEATTRIBUTE.wia_scan_contrast_percents, attribute[IMAGEATTRIBUTE.wia_scan_contrast_percents]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_vertical_scan_resolution_dpi))
setItem(item, IMAGEATTRIBUTE.wia_vertical_scan_resolution_dpi, attribute[IMAGEATTRIBUTE.wia_vertical_scan_resolution_dpi]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_vertical_scan_size_pixels))
setItem(item, IMAGEATTRIBUTE.wia_vertical_scan_size_pixels, attribute[IMAGEATTRIBUTE.wia_vertical_scan_size_pixels]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_vertical_scan_start_pixel))
setItem(item, IMAGEATTRIBUTE.wia_vertical_scan_start_pixel, attribute[IMAGEATTRIBUTE.wia_vertical_scan_start_pixel]);
}
return item;
}
/// <summary>
/// 手動調節格式圖片並返回item
/// </summary>
/// <returns>Item</returns>
public Item getHandleAdjustImageFormat(WIA.CommonDialog commonDialog, Device device)
{
return commonDialog.ShowSelectItems(device, WIA.WiaImageIntent.TextIntent, WIA.WiaImageBias.MinimizeSize, false, true, false)[1];
}
/// <summary>
/// 重新調整圖片格式
/// </summary>
/// <param name="item"></param>
/// <param name="commonDialog"></param>
/// <returns></returns>
public void AgainAdjustImageFormat(WIA.CommonDialog commonDialog, Item item)
{
commonDialog.ShowItemProperties(item, false);
}
/// <summary>
/// 顯示圖片信息
/// </summary>
/// <param name="imageFile"></param>
/// <param name="commonDialog"></param>
/// <returns></returns>
public void ShowImage(WIA.CommonDialog commonDialog, ImageFile imageFile)
{
commonDialog.ShowPhotoPrintingWizard(imageFile);
}
/// <summary>
/// ImageFile 轉換成string
/// </summary>
/// <param name="imageFile"></param>
/// <returns></returns>
public string imageFileToString(ImageFile imageFile)
{
string imageString = string.Empty;
//將獲取到得圖像轉為Image
if (imageFile != null)
{
var buffer = imageFile.FileData.get_BinaryData() as byte[];
MemoryStream ms = new MemoryStream();
ms.Write(buffer, 0, buffer.Length);
imageString = Convert.ToBase64String(buffer);
}
return imageString;
}
public Image imageFileToImage(ImageFile imageFile)
{
Image imgReturn = null;
//將獲取到得圖像轉為Image
if (imageFile != null)
{
var buffer = imageFile.FileData.get_BinaryData() as byte[];
MemoryStream ms = new MemoryStream();
ms.Write(buffer, 0, buffer.Length);
imgReturn = Image.FromStream(ms);
}
return imgReturn;
}
/// <summary>
/// Image 轉換成string
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public string imageToString(Image image)
{
string pic = null;
try
{
MemoryStream ms = new MemoryStream();
//將圖片保存成流
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
ms.Dispose();
pic = Convert.ToBase64String(arr);
}
catch (Exception)
{
return "Fail to change bitmap to string!";
}
return pic;
}
/// <summary>
/// 圖片另存為
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
private System.Drawing.Bitmap curBitmap;
public void SaveAsTo(string curFileName)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "BMP文件 (*.bmp) | *.bmp|Gif文件 (*.gif) | *.gif|JPEG文件 (*.jpg) | *.jpg|PNG文件 (*.png) | *.png";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.Title = "保存為";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
curBitmap = (Bitmap)Image.FromFile(curFileName);
string fileName = saveFileDialog1.FileName;
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
/// <summary>
/// 刪除臨時文件
/// </summary>
/// <param name="image"></param>
/// <param name="path"></param>
/// <returns></returns>
public bool deleteTemp(Image image,string path)
{
bool result = false;
if (image!=null)
{
image.Dispose();
File.Delete(path);
result = true;
}
return result;
}
}
}
wia驅動掃描儀