diff --git a/src/NTwain/DataTransferredEventArgs.cs b/src/NTwain/DataTransferredEventArgs.cs index 8671ce1..42dbbba 100644 --- a/src/NTwain/DataTransferredEventArgs.cs +++ b/src/NTwain/DataTransferredEventArgs.cs @@ -3,6 +3,9 @@ using System; namespace NTwain { + // TODO: a 2-level dispose with end of event method + // and manual dispose for perf if this is not good enough. + public class DataTransferredEventArgs : EventArgs { readonly TwainAppSession _twain; @@ -12,12 +15,13 @@ namespace NTwain /// Ctor for array data; /// /// + /// /// - internal DataTransferredEventArgs(TwainAppSession twain, byte[] data, bool isImage) + internal DataTransferredEventArgs(TwainAppSession twain, bool isImage, byte[] data) { - this._twain = twain; + _twain = twain; + _isImage = isImage; Data = data; - this._isImage = isImage; } @@ -30,25 +34,17 @@ namespace NTwain public byte[]? Data { get; } - TW_IMAGEINFO? _imgInfo; - /// - /// Gets the final image information if applicable. + /// Gets the final image information if transfer was an image. /// - public TW_IMAGEINFO? ImageInfo + public TW_IMAGEINFO? GetImageInfo() { - get + if (_isImage && _twain.GetImageInfo(out TW_IMAGEINFO info).RC == TWRC.SUCCESS) { - if (_isImage && _imgInfo == null) - { - if (_twain.GetImageInfo(out TW_IMAGEINFO info).RC == TWRC.SUCCESS) - { - _imgInfo = info; - } - } - return _imgInfo; + return info; } - } + return null; + } } -} +} \ No newline at end of file diff --git a/src/NTwain/Native/ImageTools.cs b/src/NTwain/Native/ImageTools.cs index 05e410b..67cc423 100644 --- a/src/NTwain/Native/ImageTools.cs +++ b/src/NTwain/Native/ImageTools.cs @@ -24,7 +24,7 @@ namespace NTwain.Native } - public static byte[]? GetBitmapData(System.Buffers.ArrayPool xferMemPool, IntPtr data) + public static unsafe byte[]? GetBitmapData(System.Buffers.ArrayPool xferMemPool, IntPtr data) { var infoHeader = (BITMAPINFOHEADER)Marshal.PtrToStructure(data, typeof(BITMAPINFOHEADER)); if (infoHeader.Validate()) @@ -43,12 +43,20 @@ namespace NTwain.Native var dataCopy = xferMemPool.Rent((int)fileHeader.bfSize);// new byte[fileHeader.bfSize]; - // TODO: reduce extra alloc + // TODO: run benchmark on which one is faster + // write file header - IntPtr tempPtr = Marshal.AllocHGlobal(fileHeaderSize); - Marshal.StructureToPtr(fileHeader, tempPtr, true); - Marshal.Copy(tempPtr, dataCopy, 0, fileHeaderSize); - Marshal.FreeHGlobal(tempPtr); + //IntPtr tempPtr = Marshal.AllocHGlobal(fileHeaderSize); + //Marshal.StructureToPtr(fileHeader, tempPtr, true); + //Marshal.Copy(tempPtr, dataCopy, 0, fileHeaderSize); + //Marshal.FreeHGlobal(tempPtr); + + // would this be faster? + fixed (byte* p = dataCopy) + { + Marshal.StructureToPtr(fileHeader, (IntPtr)p, false); + } + // write image Marshal.Copy(data, dataCopy, fileHeaderSize, (int)fileHeader.bfSize - fileHeaderSize); diff --git a/src/NTwain/TwainAppSession.Xfers.cs b/src/NTwain/TwainAppSession.Xfers.cs index 860e9fd..468f727 100644 --- a/src/NTwain/TwainAppSession.Xfers.cs +++ b/src/NTwain/TwainAppSession.Xfers.cs @@ -209,7 +209,7 @@ namespace NTwain { try { - var args = new DataTransferredEventArgs(this, data, true); + var args = new DataTransferredEventArgs(this, true, data); DataTransferred?.Invoke(this, args); } catch { }