All products / Saraff.Twain.NET / Contents
Using a IImageHandler for processing acquired a image data from unmanaged memory to a stream
You can override processing a acquired image data from unmanaged memory to a stream. To do this, you need to implement the IImageHandler interface and place it to the IoC container. This feature is available starting with a Saraff.Twain.NET 1.0.26.605.
// Define a IImageHandler Service class
internal sealed class _DibImageHandler:Component, IImageHandler {
private const int BufferSize = 256 * 1024;
public Stream PtrToStream(IntPtr ptr,IStreamProvider provider) {
var _stream = provider?.GetStream() ?? new MemoryStream();
var _writer = new BinaryWriter(_stream);
int _size = _DibImageHandler.GlobalSize(_DibImageHandler.GlobalHandle(ptr));
#region Write BITMAPFILEHEADER to stream
BITMAPINFOHEADER _header = Marshal.PtrToStructure(ptr,typeof(BITMAPINFOHEADER)) as BITMAPINFOHEADER;
_writer.Write((ushort)0x4d42);
_writer.Write(14 + _size);
_writer.Write(0);
_writer.Write(14 + _header.biSize + (_header.ClrUsed << 2));
#endregion
byte[] _buffer = new byte[_DibImageHandler.BufferSize];
#region Copy data to stream
for(int _offset = 0, _len = 0; _offset < _size; _offset += _len) {
_len = Math.Min(_DibImageHandler.BufferSize,_size - _offset);
Marshal.Copy((IntPtr)(ptr.ToInt64() + _offset),_buffer,0,_len);
_writer.Write(_buffer,0,_len);
}
#endregion
return _stream;
}
// ...
}
using IoC = Saraff.IoC;
//...
_container = new IoC.ServiceContainer();
_container.Bind<IStreamProvider, StreamProvider>();
// ...
_twain32 = _container.CreateInstance<Twain32>();
// ...
_twain32.Acquire();
For more details see a Saraff.Twain.NET Dependency Injection Samples.