Advanced Feature
This is a helper function when images need to be custom rendered during script execution.

It accepts an image (either existing or newly rendered one) and resizes it to the parameters passed.

Parameters

img (image) - an image object.

ToWidth (integer) - a desired maximum output width.

ToHeight (integer) - a desired maximum output height.

Return Value

image - A new Image which is not distorted and fits inside Width and Height specified
            (acts as a "best Fit")

Remarks

It is important that when images are rendered manually you detect that the rendering has indeed changed. The background script execution occurs very frequently, each keystroke or contorl focus change or clink on a placed control will trigger it. This will cause a "flicker" effect because the image is constantly being updated with the same image.

Use a format like below to avoid "flicker" by saving the variable used in the rendering of the image.

Examples

Render a simple rectangle into a picture box based on desired width and height. then scale it so that the width is always 1/2 of the picture boxes width (the height will change in ratio)

Dim w as integer = V("Width")
Dim h as integer = V("Height")
Dim ImgProps as string = w & h ' we construct a string that is built of all properties used in the rendering
' this creates a unique string representing one image.
' if more properties defined the image you would also add them with &
' this allows detecting a change by storeing the last properties values


If Session("ImgProps")<>ImgProps Then ' used to determine that the image needs to be rendered.
     Session("ImgProps") = ImgProps ' save the values that are going to be rendered
                                                     ' (so does not render again unless w or h have changed)
Dim img as new bitmap(w,h)
Dim g as Graphics = Graphics.FromImage(img) ' get a .NET rendering object for the image.
g.clear(color.yellow) ' clear the img and sets initial background to yellow.
     g.DrawRectangle(Pens.black, 0,0, w-1,h-1) ' braw a black rectangle, -1 due to 0 based rectangle

img = C.ScaleImage(img,100,100) ' scale the image to fit 100px x 100 px
    C("PictureBox1").Image = img ' set the image to a component that can show it.
End if