View on GitHub

SARAFF SOFTWARE

All products / Saraff.Twain.DS / Contents

OnImageNativeXfer

Causes the transfer of an image’s data from the Source to the application, via the Native transfer mechanism, to begin. The resulting data is stored in main memory in a single block. The data is stored in the Operating Systems native image format. The size of the image that can be transferred is limited to the size of the memory block that can be allocated by the Source. If the image is too large to fit, the Source may resize or crop the image.

Syntax

protected abstract Image OnImageNativeXfer()

Return Value

A image to transfer.

Examples

public abstract class BitmapDataSource:ImageDataSource {
    private Bitmap _currentImage;

    /// <summary>
    /// Causes the transfer of an image’s data from the Source to the application, via the Native transfer
    /// mechanism, to begin. The resulting data is stored in main memory in a single block. The data is
    /// stored in the Operating Systems native image format. The size of the image that can be transferred
    /// is limited to the size of the memory block that can be allocated by the Source. If the image is too
    /// large to fit, the Source may resize or crop the image.
    /// </summary>
    /// <returns>
    /// A image to transfer.
    /// </returns>
    protected override Image OnImageNativeXfer() {
        return this._currentImage;
    }

    /// <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
    }
}