//The scanning part of the Windows Forms code:
private void menuFileAcquire_Click(object sender, System.EventArgs e)
{
WiaClass wiaManager = null; // WIA manager COM object
CollectionClass wiaDevs = null; // WIA devices collection COM object
ItemClass wiaRoot = null; // WIA root device COM object
CollectionClass wiaPics = null; // WIA collection COM object
ItemClass wiaItem = null; // WIA image COM object
try
{
wiaManager = new WiaClass(); // create COM instance of WIA manager
wiaDevs = wiaManager.Devices as CollectionClass; // call Wia.Devices to get all devices
if ((wiaDevs == null) || (wiaDevs.Count == 0))
{
MessageBox.Show(this, "No WIA devices found!", "WIA", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
return;
}
object selectUsingUI = System.Reflection.Missing.Value; // = Nothing
wiaRoot = (ItemClass)wiaManager.Create(ref selectUsingUI); // let user select device
if (wiaRoot == null) // nothing to do
return;
// this call shows the common WIA dialog to let the user select a picture:
wiaPics = wiaRoot.GetItemsFromUI(WiaFlag.SingleImage, WiaIntent.ImageTypeColor) as CollectionClass;
if (wiaPics == null)
return;
bool takeFirst = true; // this sample uses only one single picture
foreach (object wiaObj in wiaPics) // enumerate all the pictures the user selected
{
if (takeFirst)
{
DisposeImage(); // remove previous picture
wiaItem = (ItemClass)Marshal.CreateWrapperOfType(wiaObj, typeof(ItemClass));
imageFileName = Path.GetTempFileName(); // create temporary file for image
Cursor.Current = Cursors.WaitCursor; // could take some time
this.Refresh();
wiaItem.Transfer(imageFileName, false); // transfer picture to our temporary file
pictureBox.Image = Image.FromFile(imageFileName); // create Image instance from file
menuFileSaveAs.Enabled = true; // enable "Save as" menu entry
takeFirst = false; // first and only one done.
}
Marshal.ReleaseComObject(wiaObj); // release enumerated COM object
}
}
catch (Exception ee)
{
MessageBox.Show(this, "Acquire from WIA Imaging failed\r\n" + ee.Message, "WIA", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}
finally
{
if (wiaItem != null)
Marshal.ReleaseComObject(wiaItem); // release WIA image COM object
if (wiaPics != null)
Marshal.ReleaseComObject(wiaPics); // release WIA collection COM object
if (wiaRoot != null)
Marshal.ReleaseComObject(wiaRoot); // release WIA root device COM object
if (wiaDevs != null)
Marshal.ReleaseComObject(wiaDevs); // release WIA devices collection COM object
if (wiaManager != null)
Marshal.ReleaseComObject(wiaManager); // release WIA manager COM object
Cursor.Current = Cursors.Default; // restore cursor
}
}
//The file disposing in Windows Forms:
/// temporary image file.
private string imageFileName;
/// Remove image from screen, dispose object and delete the temporary file.
private void DisposeImage()
{
menuFileSaveAs.Enabled = false; // disable "Save As" menu entry
Image oldImg = pictureBox.Image;
pictureBox.Image = null; // empty picture box
if (oldImg != null)
oldImg.Dispose(); // dispose old image (free memory, unlock file)
if (imageFileName != null)
{ // try to delete the temporary image file
try
{
File.Delete(imageFileName);
}
catch (Exception)
{ }
}
}
//The scanning part in the WPF code:
private void scanFile()
{
WiaClass wiaManager = null; // WIA manager COM object
CollectionClass wiaDevs = null; // WIA devices collection COM object
ItemClass wiaRoot = null; // WIA root device COM object
CollectionClass wiaPics = null; // WIA collection COM object
ItemClass wiaItem = null; // WIA image COM object
try
{
wiaManager = new WiaClass(); // create COM instance of WIA manager
wiaDevs = wiaManager.Devices as CollectionClass; // call Wia.Devices to get all devices
if ((wiaDevs == null) || (wiaDevs.Count == 0))
{
MessageBox.Show(this, "No WIA devices found!", "WIA", MessageBoxButton.OK, MessageBoxImage.Stop);
Application.Current.Shutdown();
return;
}
object selectUsingUI = System.Reflection.Missing.Value; // = Nothing
wiaRoot = (ItemClass)wiaManager.Create(ref selectUsingUI); // let user select device
if (wiaRoot == null) // nothing to do
return;
// this call shows the common WIA dialog to let the user select a picture:
wiaPics = wiaRoot.GetItemsFromUI(WiaFlag.SingleImage, WiaIntent.ImageTypeColor) as CollectionClass;
if (wiaPics == null)
return;
object wiaObj = wiaPics[0];
DisposeImage(); // remove previous picture
wiaItem = (ItemClass)Marshal.CreateWrapperOfType(wiaObj, typeof(ItemClass));
imageFileName = "C:\\Temp\\tmp2F6D.tmp"; // create temporary file for image
Cursor = Cursors.Wait; // could take some time
//this.Refresh();
wiaItem.Transfer(imageFileName, false); // transfer picture to our temporary file
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(imageFileName);
bitmap.EndInit();
scannedImage.Source = bitmap;
Marshal.ReleaseComObject(wiaObj); // release enumerated COM object
}
catch (Exception ee)
{
MessageBox.Show(this, "Acquire from WIA Imaging failed\r\n" + ee.Message, "WIA", MessageBoxButton.OK, MessageBoxImage.Stop);
Application.Current.Shutdown();
}
finally
{
if (wiaItem != null)
Marshal.ReleaseComObject(wiaItem); // release WIA image COM object
if (wiaPics != null)
Marshal.ReleaseComObject(wiaPics); // release WIA collection COM object
if (wiaRoot != null)
Marshal.ReleaseComObject(wiaRoot); // release WIA root device COM object
if (wiaDevs != null)
Marshal.ReleaseComObject(wiaDevs); // release WIA devices collection COM object
if (wiaManager != null)
Marshal.ReleaseComObject(wiaManager); // release WIA manager COM object
Cursor = Cursors.Arrow; // restore cursor
}
}
//The file disposing in WPF - here I need help!:
/// temporary image file.
private string imageFileName;
/// Remove image from screen, dispose object and delete the temporary file.
private void DisposeImage()
{
//This is the Windows Forms code. The WPF image name is scannedImage
//menuFileSaveAs.Enabled = false; // disable "Save As" menu entry
//Image oldImg = pictureBox.Image;
//pictureBox.Image = null; // empty picture box
//if (oldImg != null)
// oldImg.Dispose(); // dispose old image (free memory, unlock file)
//
scannedImage.Source = null; // empty the WPF image
//Here I need to do something similar to oldImg.Dispose();
if (imageFileName != null)
{ // try to delete the temporary image file
try
{
File.Delete(imageFileName);
}
catch (Exception e)
{
string ex = e.Message;
}
}
}
|