WICBitmap Reference

Reference for the WICBitmap object, which loads, converts, draws, and saves images. It uses the Windows Imaging Component (WIC).

Notation: P = property, M = method. Member names are matched case-insensitively.

Table of Contents

  1. How to Obtain
  2. Loading and Creating
  3. Saving and Encoding
  4. Size and Pixel Operations
  5. Conversion and Processing
  6. Frames and Metadata
  7. Conversion for GDI and Drawing
  8. Codec Information
  9. Events
  10. Examples

1. How to Obtain

const image = api.CreateObject("WICBitmap");   // recommended
const image2 = te.WICBitmap;                   // te.WICBitmap is also the same (a new object is created on every reference)

te.GdiplusBitmap is an old name and is deprecated (the same object is returned).

Rules (important)


2. Loading and Creating

Name Description
FromFile(path [, cx]) Loads from a file. path is a path string, a FolderItem, or a data: URI (Base64). A shortcut FolderItem is resolved to its target. For formats that cannot be read by default, loading is attempted with an archive/image plugin (te.AddEvent("GetArchive") / "GetImage"). cx is the desired size passed to the plugin (in pixels; 0 if omitted)
FromStream(stream [, fileName [, cx]]) Loads from an IStream (such as the return value of SHCreateStreamOnFileEx or GetStream). fileName and cx are hints for plugins
FromSource(stream [, bKeepStream]) Loads from an IStream. If bKeepStream is true, the original data is kept (so Save can save without re-encoding, and Frame can be switched). If you pass another WICBitmap instead of an IStream, that WICBitmap is returned as it is (it is not copied; this image is discarded). If you pass a raw WIC IWICBitmap, it becomes a copy of that image
FromHBITMAP(hbm [, hPalette [, nAlpha]]) Creates from a GDI bitmap. nAlpha: 0 = use alpha, 1 = premultiplied alpha, 2 = ignore alpha, 3 = automatic detection (the default; for 32-bit, alpha is used if any alpha value is non-zero). If a WICBitmap / IStream is passed as the argument, it is handled like FromSource
FromHICON(hIcon) Creates from an icon
FromResource(hInst, name, type, cx, cy, flags) Creates from a resource of an executable (all 6 arguments are required). The arguments are the same as hInst, name, type, cx, cy, fuLoad of LoadImage
FromArchive(dllPath, CLSID, archivePath, itemPath [, cx]) Loads an image inside an archive (a COM DLL that provides IStorage). itemPath specifies the hierarchy with \ as the separator. 4 or more arguments are required
FromClipboard([nAlpha]) Creates from the bitmap on the clipboard (CF_BITMAP). The default of nAlpha is 2. Empty if there is no bitmap
Create(width, height) Creates an empty 32bpp BGRA image of the specified size (the initial pixel values need verification; to be safe, fill it with FillRect before use). 2 arguments are required
Free() Releases the image and the stream it holds
FromItem(…) Deprecated. Does nothing

3. Saving and Encoding

The output format is determined from the string in the first argument. Both a MIME type ("image/png") and a file name / extension ("a.jpg", ".jpg") can be used. You can look up the available encoders with GetCodecInfo (Chapter 8).

Name Return value Description
Save(path [, quality]) HRESULT (integer; 0 = success, negative values = failure) Saves to a file in the format that corresponds to the extension. If no encoder is found, a failure value is returned. An existing file is overwritten
Base64([type [, quality]]) string Returns a Base64 string (without a header)
DataURI([type [, quality]]) string Returns a string in the form data:<MIME>;base64,…
GetStream([type [, quality]]) IStream Returns a memory stream of the encoded data (it can be passed to FromStream / api.ReadFile and so on)

When type is omitted: if the original data is kept (such as with bKeepStream of FromSource), it is the original format; otherwise it is image/png.

quality

Value Behavior
1 to 100 Specified as ImageQuality (the quality for JPEG and similar formats)
0 (when omitted) The default quality. However, if the original data is kept and the output format is the same as the original, the original data is copied as it is without re-encoding
100 Same as 0: if the original data is in the same format, it is copied as it is
-2 If the original data is kept, the original data is output as it is, regardless of the format (useful for saving animated GIFs, for example)

4. Size and Pixel Operations

Name Description
GetWidth() The width (px)
GetHeight() The height (px)
GetPixelFormat() The GUID string of the pixel format (such as {6FDDC324-4E03-4BFE-B185-3D77768DC90F})
GetPixel(x, y) Returns the color at (x, y) as an integer (0xAARRGGBB). Empty if out of range. Because it converts to 32bpp BGRA before reading, images in other formats can also be read (the image itself is converted)
SetPixel(x, y, color) Sets the color at (x, y) (0xAARRGGBB). Does nothing if out of range
FillRect(x, y, w, h, color [, oldColor]) Fills a rectangle with color. If you pass oldColor as the 6th argument, only the pixels that match that color are replaced. If x / y is negative, the range is treated as widened in width/height by the part that sticks out. Only the part of the range that fits inside the image is processed

5. Conversion and Processing

Name Description
GetThumbnailImage(width, height [, mode]) Returns a new WICBitmap scaled to the specified size (the original image is not changed). mode is the interpolation method (WICBitmapInterpolationMode): 0 = nearest neighbor, 1 = linear, 2 = cubic, 3 = Fant, 4 = high-quality cubic (the default). Before Windows 10, 4 is switched to 3
RotateFlip(n [, bKeepMeta]) Rotates/flips and replaces this image itself. n is WICBitmapTransformOptions: 0 = none, 1 = rotate 90°, 2 = rotate 180°, 3 = rotate 270°, 8 = flip horizontally, 16 = flip vertically (they can be combined). If bKeepMeta is not true, the kept original data and frame information are discarded (later Save calls re-encode)
Mask(color, background) Converts a black-and-white (monochrome) mask image into a colored image. The blue component of each pixel is used as the alpha value, and the color is replaced with color (the lower 24 bits of 0xRRGGBB). Pixels whose alpha is 0 are, in the implementation, replaced with the value of the first argument (the lower 24 bits, without the mask).
AlphaBlend(rect, mix [, max]) Multiplies the alpha value by mix / max in the rectangular range (a RECT from api.Memory("RECT"), or null for the whole image) (the default of max is 100). Used to make an image semi-transparent

For DrawEx, DrawText, GetHBITMAP, and GetHICON, see Chapter 7.


6. Frames and Metadata

Name Kind Description
GetFrameCount() M The number of frames (for animated GIFs and the like). 1 if there is no image
Frame P The current frame number (0-based). Assigning to it reloads that frame. It can be switched only when the original data is kept (images with multiple frames are kept automatically)
GetFrameMetadata(query) M Gets the metadata of the current frame with a WIC metadata query string (such as "/app1/ifd/{ushort=274}" or "/ifd/{ushort=306}"). Valid only when the original data is kept. Empty if not found

7. Conversion for GDI and Drawing

Name Description
GetHBITMAP([background]) Creates and returns a GDI bitmap (HBITMAP). Discard it with api.DeleteObject(hbm) when you are done. background (-1 if omitted): -1 = a 32-bit bitmap with (straight) alpha, -2 = a 32-bit bitmap with premultiplied alpha (for menu icons with alpha), -4 = the same as -2 (for the 64-bit version; for the 32-bit version before Vista, it is composited with the menu color), a value of 0 or more = an opaque bitmap alpha-composited onto that COLORREF (0x00BBGGRR) as the background
GetHICON([flags]) Creates and returns an icon (HICON). flags is ILD_* (ILD_NORMAL if omitted). Discard it with api.DestroyIcon when you are done
DrawEx(hdc, x, y, cx, cy, bkColor, fgColor, style) Draws to a device context with the arguments of ImageList_DrawEx (all 8 arguments are required). bkColor / fgColor are COLORREF values (such as CLR_NONE), and style is ILD_*. true on success
DrawText(text, fontName, size, weight, style, color, x, y) Draws text on the image (uses Direct2D / DirectWrite; all 8 arguments are required). size is the font size (DIP), weight is DWRITE_FONT_WEIGHT (400 = normal, 700 = bold), style is DWRITE_FONT_STYLE (0 = normal, 1 = oblique, 2 = italic), and color is 0xRRGGBB. It draws in the range from (x, y) to the bottom-right edge of the image. Color fonts are supported. The image is converted to the 32bpp premultiplied alpha format. In environments where Direct2D / DirectWrite cannot be used, it does nothing

8. Codec Information

GetCodecInfo(type, mode, array)

Looks up information about the codecs registered with WIC and appends the result to array (an object that has a push method, such as api.Array() or []). 3 arguments are required.

Argument Content
type The kind of component (WICComponentType): 1 = decoder, 2 = encoder, 4 = pixel format converter, 8 = metadata reader, 0x10 = metadata writer, 0x20 = pixel format
mode The information to get: 0 = file extensions (separated by ,), 1 = MIME types (separated by ,), 2 = display name, 3 = author, 4 = specification version
array Where to append the result
const ar = [];
image.GetCodecInfo(2, 1, ar);   // list of the MIME types of the encoders
// The contents of the result differ by environment (values such as `image/png` are included, separated by commas)

It can be called even when no image is held.


9. Events

Name Description
OnGetAlt A function called for paths under TE's temporary folder (%TEMP%\tablacus\) when FromFile could not load the file. Its arguments are (sessionId, name) (a session ID and a string obtained from the part of the path after the temporary folder). If it returns an alternative path (a string), loading is retried with that path

10. Examples

// Load a file, create a thumbnail, and save it as PNG
const image = api.CreateObject("WICBitmap").FromFile("C:\\Pictures\\a.jpg");
if (image) {
    const thumb = image.GetThumbnailImage(128, 128);
    thumb.Save("C:\\Temp\\thumb.png");
}

// DataURI (to embed in HTML)
const uri = image.DataURI("image/jpeg", 80);

// Take it out in the original format (without re-encoding)
const src = api.CreateObject("WICBitmap").FromSource(api.SHCreateStreamOnFileEx("C:\\Pictures\\a.gif", 0, 0, false), true);
const raw = src.DataURI("", -2);

// Create a new image and manipulate pixels
const bmp = api.CreateObject("WICBitmap").Create(100, 100);
bmp.FillRect(0, 0, 100, 100, 0xFFFF0000);   // opaque red
bmp.SetPixel(50, 50, 0xFF0000FF);           // blue
const c = bmp.GetPixel(50, 50);

// Rotate and replace (90° to the right)
image.RotateFlip(1);

// A bitmap for icons / menus
const hbm = image.GetHBITMAP(-2);
// ... after use ...
api.DeleteObject(hbm);

// Look up the available encoders
const types = [];
image.GetCodecInfo(2, 1, types);

When you pass an IStream directly, as in FromSource(api.SHCreateStreamOnFileEx(…)), adjust the call to match the arguments of api.SHCreateStreamOnFileEx (api Reference).