Displaying a PNG or TIFF image

by Richard Russell, November 2010, amended January 2012

BBC BASIC for Windows provides the *DISPLAY command to display on the screen an image contained in a BMP file, and the main Help documentation describes how to display a GIF or JPEG image. If you want to display a PNG or TIFF image you can use the code listed below:

        DEF PROCdisplay(pic$, xpos%, ypos%, xsize%, ysize%)
        LOCAL tSI{}, pic%, gdip%, image%, ix%, iy%, gfx%, lGDIP%
        DIM pic% LOCAL 513
 
        SYS "MultiByteToWideChar", 0, 0, pic$, -1, pic%, 256
 
        SYS "LoadLibrary", "GDIPLUS.DLL" TO gdip%
        IF gdip% = 0 ERROR 100, "Couldn't load GDIPLUS.DLL"
        SYS "GetProcAddress", gdip%, "GdiplusStartup" TO `GdiplusStartup`
        SYS "GetProcAddress", gdip%, "GdipLoadImageFromFile" TO `GdipLoadImageFromFile`
        SYS "GetProcAddress", gdip%, "GdipDrawImageRectRectI" TO `GdipDrawImageRectRectI`
        SYS "GetProcAddress", gdip%, "GdipGetImageHeight" TO `GdipGetImageHeight`
        SYS "GetProcAddress", gdip%, "GdipGetImageWidth" TO `GdipGetImageWidth`
        SYS "GetProcAddress", gdip%, "GdipCreateFromHDC" TO `GdipCreateFromHDC`
        SYS "GetProcAddress", gdip%, "GdipSetSmoothingMode" TO `GdipSetSmoothingMode`
        SYS "GetProcAddress", gdip%, "GdipDeleteGraphics" TO `GdipDeleteGraphics`
        SYS "GetProcAddress", gdip%, "GdipDisposeImage" TO `GdipDisposeImage`
        SYS "GetProcAddress", gdip%, "GdiplusShutdown" TO `GdiplusShutdown`
 
        DIM tSI{GdiplusVersion%, DebugEventCallback%, \
        \       SuppressBackgroundThread%, SuppressExternalCodecs%}
        tSI.GdiplusVersion% = 1
 
        SYS `GdiplusStartup`, ^lGDIP%, tSI{}, 0
        SYS `GdipLoadImageFromFile`, pic%, ^image%
        IF image% = 0 THEN
          SYS `GdiplusShutdown`, lGDIP%
          SYS "FreeLibrary", gdip%
          ERROR 90, "Couldn't load " + pic$
        ENDIF
 
        SYS `GdipGetImageWidth`, image%, ^ix%
        SYS `GdipGetImageHeight`, image%, ^iy%
 
        SYS `GdipCreateFromHDC`, @memhdc%, ^gfx%
        IF gfx%=0 ERROR 90, "GdipCreateFromHDC failed"
        SYS `GdipSetSmoothingMode`, gfx%, 2
 
        SYS `GdipDrawImageRectRectI`, gfx%, image%, xpos%, ypos%, xsize%, ysize%, \
        \                                           0, 0, ix%, iy%, 2, 0, 0, 0
 
        SYS `GdipDeleteGraphics`, gfx%
        SYS `GdipDisposeImage`, image%
        SYS `GdiplusShutdown`, lGDIP%
        SYS "FreeLibrary", gdip%
        SYS "InvalidateRect", @hwnd%, 0, 0
        SYS "UpdateWindow", @hwnd%
        ENDPROC

The parameters are the path/filename of the image file, and the required position and size of the displayed image (measured in pixels from the top left-hand corner of the output window's client area).

This code will also display BMP, GIF and JPEG images, and may do so with a better quality than the other methods. However it relies on the presence of the GDI Plus library so will work only on Windows XP (or later) or if you have specifically installed GDIPLUS.DLL on the target computer. Microsoft permits you to redistribute this file so you can include it with your program if necessary.