View on GitHub

SARAFF SOFTWARE

All products / Saraff.Twain.NET / Contents

Buffered Memory Mode Transfer

Enable buffered memory mode transfer as follows:

this._twain32.Capabilities.XferMech.Set(TwSX.Memory);

Image transmission is implemented as follows: for each image is raised SetupMemXferEvent event before obtaining, and is raised MemXferEvent event during receiving. After completion of scanning is raised AcquireCompleted event. But the ImageCount property value will be zero.

private void _twain32_SetupMemXferEvent(object sender,Twain32.SetupMemXferEventArgs e) {
    try {
        this._stream=File.Create(string.Format("MemXferTransfer_{0}.ppm",DateTime.Now.ToString("yyyyMMddHHmmss")),(int)e.BufferSize);
        var _writer=new BinaryWriter(this._stream);
        switch(e.ImageInfo.BitsPerSample.Length) {
            case 1:
                if(e.ImageInfo.PixelType==TwPixelType.Palette) {
                    _writer.Write(Encoding.ASCII.GetBytes("P6\n"));
                    this._palette=this._twain32.Palette.Get();
                } else {
                    _writer.Write(Encoding.ASCII.GetBytes("P5\n"));
                }
                break;
            case 3:
                _writer.Write(Encoding.ASCII.GetBytes("P6\n"));
                break;
            default:
                _writer.Write(Encoding.ASCII.GetBytes("PX\n"));
                break;
        }
        _writer.Write(Encoding.ASCII.GetBytes(string.Format("# (C) SARAFF SOFTWARE 2013.\n{0} {1}\n{2}\n",e.ImageInfo.ImageWidth,e.ImageInfo.ImageLength,byte.MaxValue)));
    } catch(Exception ex) {
        MessageBox.Show(ex.Message,ex.GetType().Name,MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
}
private void _twain32_MemXferEvent(object sender,Twain32.MemXferEventArgs e) {
    try {
        int _bytesPerPixel=e.ImageInfo.BitsPerPixel>>3;
        for(int i=0,_rowOffset=0; i<e.ImageMemXfer.Rows; i++,_rowOffset+=(int)e.ImageMemXfer.BytesPerRow) {
            for(int ii=0,_colOffset=0; ii<e.ImageMemXfer.Columns; ii++,_colOffset+=_bytesPerPixel) {
                switch(e.ImageInfo.BitsPerPixel) {
                    case 1:
                        for(int _mask=1; (_mask&0xff)!=0&&ii<e.ImageMemXfer.Columns; _mask<<=1,ii++) {
                            this._stream.WriteByte((e.ImageMemXfer.ImageData[_rowOffset+_colOffset]&_mask)!=0?byte.MaxValue:byte.MinValue);
                        }
                        _colOffset++;
                        ii--;
                        break;
                    case 8:
                    case 24:
                        if(e.ImageInfo.PixelType==TwPixelType.Palette) {
                            Color _color=this._palette.Colors[e.ImageMemXfer.ImageData[_rowOffset+_colOffset]];
                            this._stream.Write(new byte[] { _color.R,_color.G,_color.B },0,3);
                        } else {
                            this._stream.Write(e.ImageMemXfer.ImageData,_rowOffset+_colOffset,_bytesPerPixel);
                        }
                        break;
                }
            }
        }
    } catch(Exception ex) {
        MessageBox.Show(ex.Message,ex.GetType().Name,MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
}