我可以使用wia扫描图像。扫描后我试图附加扫描图像。下面是代码,但我收到以下错误消息:
microsoft.csharp.runtimebinder.runtimebinderexception:无法对空引用执行运行时绑定。
private async void Button_Click_3(object sender, RoutedEventArgs e)
{
lblLoading_Copy.Content = "Loading . . .";
//lblLoading.Refresh();
await Task.Delay(1000);
try
{
using (var ms = new MemoryStream())
{
var document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms).SetFullCompression();
document.Open();
foreach (System.Drawing.Image aa in obj)
{
MemoryStream msimage = new MemoryStream();
aa.Save(msimage, ImageFormat.Jpeg);
var image = iTextSharp.text.Image.GetInstance(msimage.ToArray());
image.ScaleToFit(document.PageSize.Width, document.PageSize.Height);
document.Add(image);
}
document.Close();
string Path = ConfigurationManager.AppSettings["uploadfolderpath"].ToString();//confige path
string filename = "C3kycDMS" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
//Save using drive name
//File.WriteAllBytes(Path + filename, ms.ToArray());
byte[] test = ms.ToArray();
Service1.Service objService = new Service1.Service();
string result = objService.SaveScanedDocument(test, filename, 0);
if (result == "")
{
MessageBox.Show("File Upload unsuccessfull", "Error!", MessageBoxButton.OKCancel);
lblLoading_Copy.Content = "";
}
else
{
// MessageBox.Show("File Upload successfull", "Success!", MessageBoxButton.OKCancel);
lblLoading_Copy.Content = "";
}
pic_scan.Source = null;
var hostScript = BrowserInteropHelper.HostScript;
hostScript.document.ResponseData(filename);
// return ms.ToArray();
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OKCancel);
lblLoading.Content = "";
}