Sending a scanned image.
Mon, 19 Jan 2009 15:47:19 -0500 - Author:
The following is an example of scanning an image and sending it to an e-mail address using Windows ImageAcquisition (WIA). Of course, a scanner compatible with WIA is required.
To compile the sample, add a reference to the COM library named "Microsoft Windows ImageAcquisition 1.01 Type Library" to the project, as well as the System.Drawing assembly.Then, replace the information given in the SMTP server, username, password, sender, and recipientto the correct information for your mail account, as given in the code.
The code sample is in the public domain.
To compile the sample, add a reference to the COM library named "Microsoft Windows ImageAcquisition 1.01 Type Library" to the project, as well as the System.Drawing assembly.Then, replace the information given in the SMTP server, username, password, sender, and recipientto the correct information for your mail account, as given in the code.
The code sample is in the public domain.
using System;
using WIALib;
using System.IO;
using System.Drawing;
using System.Net;
using System.Net.Mail;
using System.Runtime.InteropServices;
namespace ScanMail
{
class Program
{
public static void Main()
{
WiaClass wia=new WiaClass();
Collection coll=wia.Devices;
object device=coll[0];
Item rootItem=wia.Create(ref device);
if(device!=null){
Item item=(Item)rootItem.Children[0];
// Scan the image
item.Transfer(Directory.GetCurrentDirectory()+"image.bmp",false);
}
// Convert to PNG
using(Bitmap bm=(Bitmap)Bitmap.FromFile("image.bmp")){
bm.Save("image.png",System.Drawing.Imaging.ImageFormat.Png);
}
File.Delete("image.bmp");
SmtpClient smtp=new SmtpClient(
"smtp.example.com"); // SMTP server
smtp.Credentials=new NetworkCredential(
"sender@example.com", // Mail server username
"password" // Mail server password
);
MailMessage msg=new MailMessage(
"sender@example.com", // Sender's email address
"recipient@example.com", // Recipient's email address
"Scanned image for you", // Subject
"Here's a scan of the image as you requested." // Message
);
// Attach the PNG file
msg.Attachments.Add(new Attachment("image.png"));
smtp.Send(msg);
}
}
}
