Back to How-Tos

Resizing an image without an external application

This script uses a simplified function that demonstrates the use of the WIA object in Windows Scripting Host. It lets you resize images without the use of an external program or software.

There are a few examples of how to use the "Filters" to do different things, however note that the examples in the link below are for VBScript and need to be adjusted in syntax to be used in this file: How to use Filters

// Set the path of the source image. This should be a full path either on a
// local drive, or a UNC path for a network file.

// This example loads the current job file.
var source_file = Watch.GetJobFilename();

// You could also use a full path:
// var source_file = 'C:/path/to/image.jpg';

// This sets the output path. This can be the same as the input and if so,
// it will overwrite the original file.
var output_file = Watch.GetJobFilename();


// The following is the generic function that allows for image resize. 
// It uses the WIA object, see intro for link on more things you can do with it!
function resize(filename, newFile, maxwidth, maxheight) {
    var Img = new ActiveXObject("WIA.ImageFile");
    var IP = new ActiveXObject("WIA.ImageProcess");

    Img.LoadFile(filename);

    IP.Filters.Add(IP.FilterInfos("Scale").FilterID);
    IP.Filters(1).Properties("MaximumWidth") = maxwidth;
    IP.Filters(1).Properties("MaximumHeight") = maxheight;

    Img = IP.Apply(Img);
    Img.SaveFile(newFile);
}