mirror of
https://github.com/soukoku/ntwain.git
synced 2025-04-05 20:59:23 +08:00
Tweak native memory image getter.
This commit is contained in:
parent
416c3a80b0
commit
ebe277e614
@ -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;
|
||||
/// </summary>
|
||||
/// <param name="twain"></param>
|
||||
/// <param name="isImage"></param>
|
||||
/// <param name="data"></param>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the final image information if applicable.
|
||||
/// Gets the final image information if transfer was an image.
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -24,7 +24,7 @@ namespace NTwain.Native
|
||||
}
|
||||
|
||||
|
||||
public static byte[]? GetBitmapData(System.Buffers.ArrayPool<byte> xferMemPool, IntPtr data)
|
||||
public static unsafe byte[]? GetBitmapData(System.Buffers.ArrayPool<byte> 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);
|
||||
|
||||
|
@ -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 { }
|
||||
|
Loading…
Reference in New Issue
Block a user