View on GitHub


All products / Saraff.Twain.NET / Contents

Using a IImageFactory for processing acquired a image data from a stream to a image

You can override processing a acquired image data from a stream to a image. To do this, you need to implement the IImageFactory interface. This feature is available starting with a Saraff.Twain.NET

// Define a IImageFactory<T> Service class
internal sealed class BmpImageFactory:Component, IImageFactory<Bitmap> {

    public Bitmap Create(Stream stream) {
        switch(Environment.OSVersion.Platform) {
            case PlatformID.Unix:
                // ...
            case PlatformID.MacOSX:
                // ...
            default: // windows
                return new Bitmap(stream);
using IoC = Saraff.IoC;


_container = new IoC.ServiceContainer();
_container.Bind<IImageFactory<Bitmap>, BmpImageFactory>();

// ...

private IImageFactory<T> GetImageFactory<T>() {
    return (_container as IServiceProvider).GetService(typeof(IImageFactory<T>)) as IImageFactory<T>;

// ...

// Occurs at the end of every transfer when the application has received all the data it expected.
private void _twain32_EndXfer(object sender,Twain32.EndXferEventArgs e) {
    try {
        //this.CurrentBitmap=e.Image as Bitmap;
        // OR
    } catch(Exception ex) {
        // ...

// ...


For more details see a Saraff.Twain.NET Dependency Injection Samples.