VintaSoft Twain .NET SDK 15.4: Руководство для .NET разработчика
Vintasoft.Twain Namespace / AcquiredImage Class
Члены типа Объект Синтаксис Example Иерархия Требования Смотрите также
В этом разделе
Класс AcquiredImage
В этом разделе
Класс, представляющий изображение, полученное с TWAIN устройства.
Объектная модель
ImageInfo AcquiredImage
Синтаксис
'Declaration

Public NotInheritable Class AcquiredImage

public sealed class AcquiredImage

Пример

Вот C#/VB.NET код, который демонстрирует, как получить изображения от TWAIN устройства, обработать изображения и сохранить не пустые изображения в многостраничный TIFF файл.


''' <summary>
''' Acquires images from TWAIN device, processes images and saves only not blank images to a multipage TIFF file.
''' </summary>
Public Shared Sub AcquireImagesFromTwainDeviceAndProcessImages()
    ' create the device manager
    Using deviceManager As New Vintasoft.Twain.DeviceManager()
        ' open the device manager
        deviceManager.Open()

        ' select the device in the default device selectio ndialog
        deviceManager.ShowDefaultDeviceSelectionDialog()

        ' get reference to the selected device
        Dim device As Vintasoft.Twain.Device = deviceManager.DefaultDevice

        ' specify that device UI must not be used
        device.ShowUI = False
        ' specify that device must be closed after scan
        device.DisableAfterAcquire = True

        Dim tiffFilename As String = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "multipage.tif")

        ' acquire images from device
        Dim acquireModalState As Vintasoft.Twain.AcquireModalState = Vintasoft.Twain.AcquireModalState.None
        Dim acquiredImage As Vintasoft.Twain.AcquiredImage
        Do
            acquireModalState = device.AcquireModal()
            Select Case acquireModalState
                Case Vintasoft.Twain.AcquireModalState.ImageAcquired
                    ' get reference to the image acquired from device
                    acquiredImage = device.AcquiredImage

                    ' despeckle/deskew/detect border
                    ProcessAcquiredImage(acquiredImage)
                    ' add image to multipage TIFF file if image is not blank
                    If Not acquiredImage.IsBlank(0.01F) Then
                        acquiredImage.Save(tiffFilename)
                    End If

                    ' dispose acquired image
                    acquiredImage.Dispose()
                    Exit Select
            End Select
        Loop While acquireModalState <> Vintasoft.Twain.AcquireModalState.None

        ' close the device
        device.Close()

        ' close the device manager
        deviceManager.Close()
    End Using
End Sub

Private Shared Sub ProcessAcquiredImage(acquiredImage As Vintasoft.Twain.AcquiredImage)
    System.Console.WriteLine(String.Format("Image ({0})", acquiredImage.ImageInfo))

    Try
        ' subscribe to processing events
        AddHandler acquiredImage.Processing, New System.EventHandler(Of Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingEventArgs)(AddressOf acquiredImage_Processing)
        AddHandler acquiredImage.Progress, New System.EventHandler(Of Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingProgressEventArgs)(AddressOf acquiredImage_Progress)
        AddHandler acquiredImage.Processed, New System.EventHandler(Of Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessedEventArgs)(AddressOf acquiredImage_Processed)

        ' despeckle/deskew/detect border
        acquiredImage.Despeckle(8, 25, 30, 400)
        acquiredImage.Deskew(Vintasoft.Twain.ImageProcessing.TwainBorderColor.AutoDetect, 5, 5)
        acquiredImage.DetectBorder(5)
    Catch ex As System.Exception
        System.Console.WriteLine("Error: " & ex.Message)
    Finally
        ' unsubscribe from processing events
        RemoveHandler acquiredImage.Processing, New System.EventHandler(Of Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingEventArgs)(AddressOf acquiredImage_Processing)
        RemoveHandler acquiredImage.Progress, New System.EventHandler(Of Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingProgressEventArgs)(AddressOf acquiredImage_Progress)
        RemoveHandler acquiredImage.Processed, New System.EventHandler(Of Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessedEventArgs)(AddressOf acquiredImage_Processed)
    End Try
End Sub

Private Shared Sub acquiredImage_Processing(sender As Object, e As Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingEventArgs)
    System.Console.Write(e.Action.ToString() & " ")
End Sub

Private Shared Sub acquiredImage_Progress(sender As Object, e As Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingProgressEventArgs)
    System.Console.Write(".")
End Sub

Private Shared Sub acquiredImage_Processed(sender As Object, e As Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessedEventArgs)
    System.Console.WriteLine(" finished")
End Sub


/// <summary>
/// Acquires images from TWAIN device, processes images and saves only not blank images to a multipage TIFF file.
/// </summary>
public static void AcquireImagesFromTwainDeviceAndProcessImages()
{
    // create the device manager
    using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
    {
        // open the device manager
        deviceManager.Open();

        // select the device in the default device selectio ndialog
        deviceManager.ShowDefaultDeviceSelectionDialog();

        // get reference to the selected device
        Vintasoft.Twain.Device device = deviceManager.DefaultDevice;

        // specify that device UI must not be used
        device.ShowUI = false;
        // specify that device must be closed after scan
        device.DisableAfterAcquire = true;

        string tiffFilename = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "multipage.tif");

        // acquire images from device
        Vintasoft.Twain.AcquireModalState acquireModalState = Vintasoft.Twain.AcquireModalState.None;
        Vintasoft.Twain.AcquiredImage acquiredImage;
        do
        {
            acquireModalState = device.AcquireModal();
            switch (acquireModalState)
            {
                case Vintasoft.Twain.AcquireModalState.ImageAcquired:
                    // get reference to the image acquired from device
                    acquiredImage = device.AcquiredImage;

                    // despeckle/deskew/detect border
                    ProcessAcquiredImage(acquiredImage);
                    // add image to multipage TIFF file if image is not blank
                    if (!acquiredImage.IsBlank(0.01f))
                        acquiredImage.Save(tiffFilename);

                    // dispose acquired image
                    acquiredImage.Dispose();
                    break;
            }
        }
        while (acquireModalState != Vintasoft.Twain.AcquireModalState.None);

        // close the device
        device.Close();

        // close the device manager
        deviceManager.Close();
    }
}

private static void ProcessAcquiredImage(Vintasoft.Twain.AcquiredImage acquiredImage)
{
    System.Console.WriteLine(string.Format("Image ({0})", acquiredImage.ImageInfo));

    try
    {
        // subscribe to processing events
        acquiredImage.Processing += new System.EventHandler<Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingEventArgs>(acquiredImage_Processing);
        acquiredImage.Progress += new System.EventHandler<Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingProgressEventArgs>(acquiredImage_Progress);
        acquiredImage.Processed += new System.EventHandler<Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessedEventArgs>(acquiredImage_Processed);

        // despeckle/deskew/detect border
        acquiredImage.Despeckle(8, 25, 30, 400);
        acquiredImage.Deskew(Vintasoft.Twain.ImageProcessing.TwainBorderColor.AutoDetect, 5, 5);
        acquiredImage.DetectBorder(5);
    }
    catch (System.Exception ex)
    {
        System.Console.WriteLine("Error: " + ex.Message);
    }
    finally
    {
        // unsubscribe from processing events
        acquiredImage.Processing -= new System.EventHandler<Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingEventArgs>(acquiredImage_Processing);
        acquiredImage.Progress -= new System.EventHandler<Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingProgressEventArgs>(acquiredImage_Progress);
        acquiredImage.Processed -= new System.EventHandler<Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessedEventArgs>(acquiredImage_Processed);
    }
}

static void acquiredImage_Processing(object sender, Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingEventArgs e)
{
    System.Console.Write(e.Action.ToString() + " ");
}

static void acquiredImage_Progress(object sender, Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingProgressEventArgs e)
{
    System.Console.Write(".");
}

static void acquiredImage_Processed(object sender, Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessedEventArgs e)
{
    System.Console.WriteLine(" finished");
}

Иерархия наследования

System.Object
   Vintasoft.Twain.AcquiredImage

Требования

Целевые платформы: .NET 10; .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

Смотрите также