Resources Base
Table of contents


How to automatically deskew an image

Generally, there are two ways to automatically deskew an image.

Enable the capability of a scanner

Applicable only to compatible TWAIN scanners

There is a standard TWAIN capability called ICAP_AUTOMATICDESKEW which, when enabled, does the deskewing of all scanned images automatically. If your scanner supports this capability, you can enable the functionality through DWT using the API IfAutomaticDeskew

DWObject.IfAutomaticDeskew = true;

Use DWT to deskew an image as it is scanned

The function deskew() below is applicable to all platforms. The event OnPostTransferAsync is only triggered during scanning

function deskew(index) {
        function(angle) {
            console.log("skew angle: " + angle);
            DWObject.Rotate(index, angle, true,
                function() {
                    console.log("Successfully deskewed an image!");
                function(errorCode, errorString) {
        function(errorCode, errorString) {
DWObject.RegisterEvent("OnPostTransferAsync", function(info) {

How to insert images to a specified index

By default, when you scan or load images, they are appended to the end of the image array in buffer. However, in some business scenarios, the user might want to insert these new images to a specified index. Unfortunately, DWT doesn’t provide a native method for that. The following code snippet shows how it can be done

Insert when acquiring

function acquireToIndex(index) {

    DWObject.IfAppendImage = false;
    DWObject.CurrentImageIndexInBuffer = index;
    DWObject.RegisterEvent('OnPostTransfer', function() {
    DWObject.RegisterEvent('OnPostAllTransfers', function() {
        DWObject.IfAppendImage = true;


Insert when loading

function loadToIndex(index) {

    var oldCount = DWObject.HowManyImagesInBuffer;
    DWObject.RegisterEvent('OnPostLoad', function() {
        var newCount = DWObject.HowManyImagesInBuffer;
        for (var j = 0; j < newCount - oldCount; j++)
            DWObject.MoveImage(oldCount + j, index + j);
    DWObject.LoadImageEx('', 5);


How to detect and discard blank pages automatically?

If the TWAIN driver of your device supports discarding blank pages, you can use the driver’s built-in feature.

  1. You can set the IfShowUI property to true to display the User Interface (UI) of the source and you can check the option there (it normally reads ‘discard blank’)
  2. If you don’t want to show the user interface of the source, you can set IfAutoDiscardBlankpages to true or negotiate the ICAP_AUTODISCARDBLANKPAGES capability in code to discard blank page automatically. Please NOTE that this property or capability only works if the scanner itself supports the feature (on the hardware level).
DWObject.IfShowUI = false;
//*Use the property
DWObject.IfAutoDiscardBlankpages = true;
//*Use capability negotiation
DWObject.CapType = EnumDWT_CapType.TWON_ONEVALUE;
DWObject.CapValue = -1;//Auto

If the scanner itself doesn’t support discarding blank pages, you can also use the IsBlankImageExpress method to do this as a workaround. To detect and discard blank pages automatically, you can do it in the OnPostTransfer event which fires after each transfer.

function DWObject_OnPostTransfer() {
DWObject.BlankImageMaxStdDev = 20;
if (DWObject.IsBlankImageExpress(DWObject.CurrentImageIndexInBuffer)) {

NOTE: In many cases, the scanned blank image may come with some noises which would affect the result returned by IsBlankImageExpress. To improve the result, you may adjust the value of BlankImageMaxStdDev Property. The default value is 1 (0 means single-color image). Thus, by increasing the value a little bit (e.g. to 20), noises on images are ignored so a blank image can be detected faster.

Is this page helpful?

YesYes NoNo

In this article:

latest version

    • Latest Version
    • Version 16.1.1
    Change +
    © 2003–2020 Dynamsoft. All rights reserved.
    Privacy Statement / Site Map / Home / Purchase / Support