View on GitHub

SARAFF SOFTWARE

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 1.0.27.606.

// 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
        this.CurrentBitmap=e.CreateImage(this.GetImageFactory<Bitmap>());
    } catch(Exception ex) {
        // ...
    }
}

// ...

_twain32.Acquire();

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