C# .NET - scan and save the image

Asked By gnanam gnanam on 11-Oct-08 02:38 AM

i want to scan the patient at reception and capture the patient image(face) only then then i store the image and retrive the image.

i know how to store the image using upload option but i dont know how to get the image from camera.

pls send the coding .it is very urgent

save the image

Binny ch replied to gnanam gnanam on 11-Oct-08 02:45 AM
protected void Button1_Click(object sender, EventArgs e)
{
FileInfo imageInfo = new FileInfo(File1.Value.Trim());

if (!imageInfo.Exists)
this.RegisterClientScriptBlock("alertMsg", "<script>alert('please select one image file.');</script>");
else
{
switch (imageInfo.Extension.ToUpper())
{
case ".JPG": this.UpLoadImageFile(imageInfo); break;
case ".GIF": this.UpLoadImageFile(imageInfo); break;
case ".BMP": this.UpLoadImageFile(imageInfo); break;
default: this.RegisterClientScriptBlock("alertMsg", "<script>alert('file type error.');</script>"); break;
}
}
}

private void UpLoadImageFile(FileInfo info)
{
SqlConnection objConn = null;
SqlCommand objCom = null;
try
{
byte[] content = new byte[info.Length];
FileStream imagestream = info.OpenRead();
imagestream.Read(content, 0, content.Length);
imagestream.Close();

objConn = new SqlConnection(strConnectionString);
objCom = new SqlCommand("insert into Categories(CategoryName,Picture)values(@CategoryName,@Picture)", objConn);

SqlParameter categorynameParameter = new SqlParameter("@CategoryName", SqlDbType.NVarChar);
if (this.txtFileName.Text.Trim().Equals(""))
categorynameParameter.Value = "Default";
else
categorynameParameter.Value = this.txtFileName.Text.Trim();
objCom.Parameters.Add(categorynameParameter);

SqlParameter pictureParameter = new SqlParameter("@Picture", SqlDbType.Image);
pictureParameter.Value = content;
objCom.Parameters.Add(pictureParameter);

objConn.Open();
objCom.ExecuteNonQuery();
objConn.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
objConn.Close();
}
}

protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection objConn = null;
try
{
objConn = new SqlConnection(strConnectionString);

SqlCommand Command = new SqlCommand("select * from Categories order by CategoryID DESC", objConn);
objConn.Open();
SqlDataReader MyReader = Command.ExecuteReader(CommandBehavior.CloseConnection);

if (MyReader.HasRows == true)
{
MyReader.Read();

Response.ContentType = "text/HTML";
Response.BinaryWrite((byte[])MyReader["Picture"]);
}
else
{
this.RegisterClientScriptBlock("alertMsg", "<script>alert('No Image.');</script>");
} MyReader.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
See this link:
http://www.aspnettutorials.com/tutorials/database/Save-Img-ToDB-Csharp.aspx

Use WIA

Sagar P replied to gnanam gnanam on 11-Oct-08 03:32 AM

You can do it by using WIA;

This provides step-by-step instructions to use Microsoft Windows Image Acquisition (WIA) in real-world applications.

The presents code from the sample applications included in the SDK. It is intended to demonstrate how developers can implement the major functionality of WIA and is not a blueprint to create a complete application.

The focuses on the following tasks:

  • http://msdn.microsoft.com/en-us/library/ms629848(VS.85).aspx
  • http://msdn.microsoft.com/en-us/library/ms629852(VS.85).aspx
  • http://msdn.microsoft.com/en-us/library/ms629854(VS.85).aspx
  • http://msdn.microsoft.com/en-us/library/ms629846(VS.85).aspx
  • http://msdn.microsoft.com/en-us/library/ms629850(VS.85).aspx
  • http://msdn.microsoft.com/en-us/library/ms629858(VS.85).aspx
  • http://msdn.microsoft.com/en-us/library/ms629856(VS.85).aspx
  • http://msdn.microsoft.com/en-us/library/ms629844(VS.85).aspx

Best Luck!!!!!!!!!!!!!!!!
Sujit.

solution

Perry replied to gnanam gnanam on 11-Oct-08 03:55 AM
Hi,

Use below code to scan the image using digital camera.

using
System.Runtime.InteropServices;
using WIALib;
// WIA manager COM object.
// Allows the user to select an imaging device like scanner/camera etc.
WiaClass wiaManager = null;
 
// WIA devices collection COM object.
// The collection of imaging devices.
CollectionClass wiaDevicesCollection = null;
 
// WIA root device COM object.
// Represents the selected imaging device.
ItemClass wiaRootDeviceItem = null;
 
// WIA collection COM object.
// Collection of WIA Image items.
CollectionClass wiaImageItems = null;
 
// WIA image COM object.
// Represents the first of our selected image.
ItemClass wiaFirstScannedItem = null;
 
try
{
// create COM instance of WIA manager
wiaManager = new WiaClass();
 
// call Wia.Devices to get all devices
wiaDevicesCollection = (CollectionClass)wiaManager.Devices;
 
// No Devices found.
if (null == wiaDevicesCollection || 0 == wiaDevicesCollection.Count)
{
throw new Exception("No WIA devices found!");
}
 
// = Nothing for COM.
object useDialogFlag = System.Reflection.Missing.Value;
 
// User will select a root device here.
wiaRootDeviceItem = (ItemClass)wiaManager.Create(ref useDialogFlag);
 
// No device selected. Just return.
if (null == wiaRootDeviceItem)
{
return;
}
 
// Get the list of images.
wiaImageItems = (CollectionClass)wiaRootDeviceItem.GetItemsFromUI(WiaFlag.SingleImage, WiaIntent.ImageTypeColor);
 
// If there is a problem, return.
if (null == wiaImageItems)
{
return;
}
 
// We'll grab the first picture only.
bool useFirstImageOnly = true;
 
// Iterate through the images and select the first one.
foreach (object wiaImageObject in wiaImageItems)
{
if ((useFirstImageOnly))
{
wiaFirstScannedItem = (ItemClass)Marshal.CreateWrapperOfType(wiaImageObject, typeof(ItemClass));
 
// Get a temporary file name.
string tempFileName = System.IO.Path.GetTempFileName();
 
// Copy the scanned object to the temporary file, in a synchronous manner.
wiaFirstScannedItem.Transfer(tempFileName, false);
 
// Work with the tempFile.
 
useFirstImageOnly = false;
 
// If you want to get hold of all the scanned items,
// remove the 'useFirstImageOnly' flag.
}
 
// Release the enumerated COM object image.
Marshal.ReleaseComObject(wiaImageObject);
}
}
catch (Exception ex)
{
throw new Exception("Acquire from WIA Imaging failed: " + ex.Message);
}
finally
{
// Release the COM objects used.
 
if (null != wiaFirstScannedItem)
{
Marshal.ReleaseComObject(wiaFirstScannedItem);
}
 
if (null != wiaImageItems)
{
Marshal.ReleaseComObject(wiaImageItems);
}
 
if (null != wiaRootDeviceItem)
{
Marshal.ReleaseComObject(wiaRootDeviceItem);
}
 
if (null != wiaDevicesCollection)
{
Marshal.ReleaseComObject(wiaDevicesCollection);
}
 
if (null != wiaManager)
{
Marshal.ReleaseComObject(wiaManager);
}
}

See http://www.codeproject.com/KB/dotnet/wiascriptingdotnet.aspx and http://rajanadar.com/2008/05/scan-using-wia/ for details.

Regards,
Megha
scan & save
C_A P replied to gnanam gnanam on 11-Oct-08 06:21 AM

The Code

I’ve created a Windows application with three buttons to keep this simple.

image001.jpg
  • Select Scanner – Allows you to choose a scanning device on your local machine.
  • Scan – Initiates the scanning process.
  • Save – Saves the results of the scanned images after having been OCR’d.

Under the hood, the LEADTOOLS .NET classes perform the bulk of the work. We’ll walk through the code in the order that it’s executed, starting with the form load event.

Collapse
private void MainFrm_Load(object sender, EventArgs e)
{
// Unlock Support for features
RasterSupport.Unlock(RasterSupportType.Document, "");
RasterSupport.Unlock(RasterSupportType.Ocr, "");
RasterSupport.Unlock(RasterSupportType.OcrPdfOutput, "");

// Create objects
_twSession = new TwainSession();
_OCR = RasterDocumentEngine.Instance;
_Deskew = new DeskewCommand();
_Despeckle = new DespeckleCommand();
_HoleRemove = new HolePunchRemoveCommand();
_BorderRemove = new BorderRemoveCommand();
_LineRemove = new LineRemoveCommand();
_SmoothCharacters = new SmoothCommand();
_InvertText = new InvertedTextCommand();

// Initialize Twain Object
_twSession.Startup(this, "LEAD Technologies, Inc", "Tutorials",
"1.0.0.0", "ScanOCRSavePDF", TwainStartupFlags.None);
_twSession.AcquirePage += new EventHandler(_twSession_AcquirePage);

// Initialize OCR Object
_OCR.Startup();
_OCR.RecognitionDataFileName = Environment.GetEnvironmentVariable("TEMP") +
"\\ocrRdf.rdf";
RasterDocumentResultOptions opts = _OCR.SaveResultOptions;
opts.Format = RasterDocumentFormatType.PdfImageOnText;
opts.FormatLevel = RasterDocumentFormatLevel.Full;
_OCR.SaveResultOptions = opts;

// Initialize Deskew object
_Deskew.FillColor = new RasterColor(Color.White);
_Deskew.Flags = DeskewCommandFlags.DeskewImage |
DeskewCommandFlags.DocumentAndPictures | DeskewCommandFlags.RotateBicubic;

// Initialize Holepunch Remove object
_HoleRemove.Flags = HolePunchRemoveCommandFlags.UseCount |
HolePunchRemoveCommandFlags.UseLocation |
HolePunchRemoveCommandFlags.UseDpi;
_HoleRemove.Location = HolePunchRemoveCommandLocation.Left;
_HoleRemove.MaximumHoleCount = 5;
_HoleRemove.MinimumHoleCount = 2;

// Initialize InvertedText object
_InvertText.Flags = InvertedTextCommandFlags.UseDpi;
_InvertText.MinimumInvertWidth = 6000;
_InvertText.MinimumInvertHeight = 186;
_InvertText.MaximumBlackPercent = 95;
_InvertText.MinimumBlackPercent = 75;

// Initialize BorderRemove object
_BorderRemove.Border = BorderRemoveBorderFlags.All;
_BorderRemove.Percent = 20;
_BorderRemove.WhiteNoiseLength = 9;
_BorderRemove.Variance = 3;

// Initialize LineRemove object
_LineRemove.Flags = LineRemoveCommandFlags.UseGap |
LineRemoveCommandFlags.UseVariance;
_LineRemove.GapLength = 2;
_LineRemove.MaximumLineWidth = 8;
_LineRemove.MaximumWallPercent = 10;
_LineRemove.MinimumLineLength = 200;
_LineRemove.Variance = 2;
_LineRemove.Wall = 14;

// Initialize SmoothCharacters object
_SmoothCharacters.Flags = SmoothCommandFlags.None;
_SmoothCharacters.Length = 1;
}

I first unlock the support for some of the Document Imaging Suite features. These functions only have to be called once (typically in a startup routine) and the features they unlock are then available for the life of the process. If you are using the LEADTOOLS evaluation, you do not have to call these functions, as all functionality is available.

Next we create each object. The OCR object (RasterDocumentEngine) and the scanning object (TwainSession) are created globally, as we’ll need them in multiple functions. The rest of the objects are used to clean the images as they are scanned into the application. They are created globally to avoid having to create and destroy them over and over for each page scanned.

For both the scanning object and OCR object, you must call the StartUp function before you can begin using them.

I link a function to the AcquirePage event in the scanning object. This event is called for each page captured by the scanner.

In this sample, we are saving out the text that the OCR object has generated from the images (recognized text) as a searchable PDF (PDF Image with text underneath). We also set the RecognitionDataFileName to a file in the user’s temp directory. This file is used by the OCR engine to store the recognized text before it is converted to a final format, such as Microsoft Word, Excel, PDF, etc. Each time you OCR an image, it appends the recognized text to this file. This would allow you to append multiple documents together even if you restart your machine in between scans. To opt out of this option, simply delete this file prior to starting the recognition process.

Each document clean object is then initialized to values that are optimal for most scanned bitonal images.

private void btnSelectScanner_Click(object sender, EventArgs e)
{
_twSession.SelectSource(string.Empty);
}

In the btnSelectScanner_Click event, simply call TwainSession::SelectSource with an empty string to display the SelectSource dialog. This dialog is populated by the Twain Source Manager found in the twain32.dll file.

image005.jpg

If you would like to select a scanning device without showing this dialog, simply pass the name of the device for the parameter in the SelectSource function.

Before we begin the scan, you'll want to set up the scanner to produce images that are optimal for OCR. We set the X and Y resolution to 300 and set the bits per pixel to one, which essentially tells the scanning device to scan in black and white.

Next, _twSession.Acquire begins the scanning process. In this sample, we passed "None" as a parameter, which means that no other user interface will appear before the scanner begins capturing. You can also pass "Show" to show the scanner’s dialog, which will allow the user to have the final say on the settings used.

Here is the code that does what was just described:

Collapse
private void btnScan_Click(object sender, EventArgs e)
{
try
{
// Change Cursor to Wait
this.Cursor = Cursors.WaitCursor;

// Set scanner to acquire with optimal settings for OCR
_twSession.Resolution = new SizeF(300.0f, 300.0f);
_twSession.ImageBitsPerPixel = 1;

// Scan from scanner.
_twSession.Acquire(TwainUserInterfaceFlags.None);

// AutoOrient each page
Console.WriteLine("AutoOrientPage");
for (int i = 0; i < _OCR.PageCount; i++)
{
_OCR.AutoOrientPage(i);
}

// Delete RDF file so we do not constantly append to it.
if (System.IO.File.Exists(_OCR.RecognitionDataFileName))
System.IO.File.Delete(_OCR.RecognitionDataFileName);

// OCR all the pages
Console.WriteLine("Recognize");
_OCR.Recognize(0, _OCR.PageCount, null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// Change cursor to arrow
this.Cursor = Cursors.Arrow;
}
}

At this point, the images are being scanned and the AcquirePage event is being called for each page scanned. This event is covered further down. Once the scan is complete, we call AutoOrientPage for each page in the OCR. If a page was scanned up-side-down, this function will rotate it back to right-side-up. Next, we delete the recognition data file if it exists and then recognize all of the pages.

The _twSession_AcquirePage event is called for each page that is scanned. The scanned image is given to you in the TwainAcquirePageEventArgs::Image parameter. In this event, we clean up the image using each of the document clean-up classes created and set up in the form load event. Once the image is clean, we add it to the OCR object where it is later converted to editable text and stored in the recognition data file.

Collapse
void _twSession_AcquirePage(object sender, TwainAcquirePageEventArgs e)
{
// Clean image before adding to OCR object

// Deskew
_Deskew.Run(e.Image);

// Despekle
_Despeckle.Run(e.Image);

// Hole Punch Remove
_HoleRemove.Run(e.Image);

// Inverted Text
_InvertText.Run(e.Image);

// Border Remove
_BorderRemove.Run(e.Image);

// Line Remove
_LineRemove.Type = LineRemoveCommandType.Vertical;
_LineRemove.Run(e.Image);
_LineRemove.Type = LineRemoveCommandType.Horizontal;
_LineRemove.Run(e.Image);

// Smooth Characters
_SmoothCharacters.Run(e.Image);

// Add Page to OCR
_OCR.AddPage(e.Image, -1);
}

Lastly, I save the results from the OCR to disk. As you remember, I set up the OCR to output the results as a PDF file. The OCR will take the data in the recognition data file and convert it to a searchable PDF file.

private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "PDF (*.pdf)|*.pdf";
dlg.FilterIndex = 0;

if (dlg.ShowDialog() == DialogResult.OK)
{
_OCR.SaveResultsToFile(dlg.FileName);
}
}