using NTwain.Data; using NTwain.Internals; using NTwain.Interop; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Globalization; using System.Linq; using System.Runtime.InteropServices; using System.Security.Permissions; namespace NTwain { /// /// Contains event data after whatever data from the source has been transferred. /// public class DataTransferredEventArgs : EventArgs { /// /// Initializes a new instance of the class. /// /// The source. /// The native data. /// The image information. public DataTransferredEventArgs(DataSource source, IntPtr nativeData, TWImageInfo imageInfo) { DataSource = source; NativeData = nativeData; ImageInfo = imageInfo; } /// /// Initializes a new instance of the class. /// /// The source. /// The file data path. /// The image information. /// The image file format. public DataTransferredEventArgs(DataSource source, string fileDataPath, TWImageInfo imageInfo, FileFormat imageFileFormat) { DataSource = source; FileDataPath = fileDataPath; ImageInfo = imageInfo; ImageFileFormat = imageFileFormat; } /// /// Initializes a new instance of the class. /// /// The source. /// The memory data. /// The image information. public DataTransferredEventArgs(DataSource source, byte[] memoryData, TWImageInfo imageInfo) { DataSource = source; MemoryData = memoryData; ImageInfo = imageInfo; } /// /// Gets pointer to the complete data if the transfer was native. /// The data will be freed once the event handler ends /// so consumers must complete whatever processing before then. /// For image type this data is DIB (Windows) or TIFF (Linux). /// This pointer is already locked for the duration of this event. /// /// The data pointer. public IntPtr NativeData { get; private set; } /// /// Gets the file path to the complete data if the transfer was file or memory-file. /// /// /// The file path. /// public string FileDataPath { get; private set; } /// /// Gets the file format if applicable. /// /// /// The file format. /// public FileFormat ImageFileFormat { get; private set; } /// /// Gets the raw memory data if the transfer was memory. /// Consumer application will need to do the parsing based on the values /// from . /// /// /// The memory data. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] public byte[] MemoryData { get; private set; } /// /// Gets the final image information if applicable. /// /// /// The final image information. /// public TWImageInfo ImageInfo { get; private set; } /// /// Gets the data source. /// /// /// The data source. /// public DataSource DataSource { get; private set; } /// /// Gets the extended image information if applicable. /// /// The information ids. /// /// public IEnumerable GetExtImageInfo(params ExtendedImageInfo[] infoIds) { if (infoIds != null && infoIds.Length > 0)// && DataSource.SupportedCaps.Contains(CapabilityId.ICapExtImageInfo)) { var request = new TWExtImageInfo { NumInfos = (uint)infoIds.Length }; if (infoIds.Length > request.Info.Length) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Info ID array exceeded maximum length of {0}.", request.Info.Length)); } for (int i = 0; i < infoIds.Length; i++) { request.Info[i].InfoID = infoIds[i]; } if (DataSource.DGImage.ExtImageInfo.Get(request) == ReturnCode.Success) { return request.Info.Where(it => it.InfoID != ExtendedImageInfo.Invalid); } } return Enumerable.Empty(); } /// /// Gets the bitmap from the if it's an image. /// /// public Image GetNativeImage() { Image image = null; if (NativeData != IntPtr.Zero) { if (ImageTools.IsDib(NativeData)) { image = ImageTools.ReadBitmapImage(NativeData); } else if (ImageTools.IsTiff(NativeData)) { image = ImageTools.ReadTiffImage(NativeData); } } return image; } } }