View on GitHub

SARAFF SOFTWARE

All products / Saraff.Twain.DS / Contents

OnImageFileXfer

This operation is used to initiate the transfer of an image from the Source to the application via the disk-file transfer mechanism. It causes the transfer to begin.

Syntax

protected virtual void OnImageFileXfer()

Examples

public abstract class BitmapDataSource:ImageDataSource {
    private Bitmap _currentImage;

    /// <summary>
    /// This operation is used to initiate the transfer of an image from the Source to the application via the
    /// disk-file transfer mechanism. It causes the transfer to begin.
    /// </summary>
    protected override void OnImageFileXfer() {
        using(var _stream=File.Open(this.XferEnvironment.FileXferName,FileMode.Create)) {
            this._currentImage.Save(
                _stream,
                new Dictionary<TwFF,ImageFormat> {
                    {TwFF.Bmp,ImageFormat.Bmp},
                    {TwFF.Exif,ImageFormat.Exif},
                    {TwFF.Jfif,ImageFormat.Jpeg},
                    {TwFF.Png,ImageFormat.Png},
                    {TwFF.Tiff,ImageFormat.Tiff},
                    {TwFF.TiffMulti,ImageFormat.Tiff}
                }[this.XferEnvironment.FileXferFormat]);
        }
    }

    /// <summary>
    /// Invoked to indicate that the Source has data that is ready to be transferred.
    /// </summary>
    protected override void OnXferReady() {
        if(this.XferEnvironment.PendingXfers==0) {
            this.XferEnvironment.PendingXfers=(ushort)Math.Abs((short)this[TwCap.XferCount].Value);
        }
        this._Acquire();

        base.OnXferReady();
    }

    /// <summary>
    /// Invoked at the end of every transfer to signal that the application has received all the data it expected.
    /// </summary>
    protected override void OnEndXfer() {
        this._Dispose();
        if(this.XferEnvironment.PendingXfers>0) {
            this._Acquire();
        }

        base.OnEndXfer();
    }

    /// <summary>
    /// Invoked when the pending transfers discarded.
    /// </summary>
    protected override void OnResetXfer() {
        this._Dispose();

        base.OnResetXfer();
    }

    private void _Dispose() {
        if(this._currentImage!=null) {
            this._currentImage.Dispose();
            this._currentImage=null;
        }
    }

    /// <summary>
    /// Acquire bitmap image.
    /// </summary>
    /// <returns>The bitmap image.</returns>
    protected abstract Bitmap Acquire();

    private void _Acquire() {
        this._BitmapReader=null;
        this._currentImage=this.Acquire();

        #region ImageInfo

        this.XferEnvironment.ImageInfo=new ImageInfo {
            BitsPerPixel=(short)Image.GetPixelFormatSize(this._currentImage.PixelFormat),
            BitsPerSample=new Dictionary<PixelFormat, short[]> {
                {PixelFormat.Format1bppIndexed,new short[] { 1 }},
                {PixelFormat.Format4bppIndexed,new short[] { 4 }},
                {PixelFormat.Format8bppIndexed,new short[] { 8 }},
                {PixelFormat.Format24bppRgb,new short[] { 8, 8, 8 }},
                {PixelFormat.Format48bppRgb,new short[] { 16, 16, 16 }}}[this._currentImage.PixelFormat],
            Compression=TwCompression.None,
            ImageLength=this._currentImage.Height,
            ImageWidth=this._currentImage.Width,
            PixelType=(TwPixelType)this[TwCap.IPixelType].Value,
            Planar=false,
            XResolution=this._currentImage.HorizontalResolution,
            YResolution=this._currentImage.VerticalResolution
        };

        #endregion
    }
}