.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;
}
}
}