lundi 17 avril 2017

Resizing image

Resizing Image


A recurrent question by newbies is how to resize a window containing a Red Image. Once again, due to the elegance of Red language, this is really easy to do.

The resize flag

When creating a window, Red allows different flags such as resize flag which allows the window to be scaled in response to user's action :  view/flags/no-wait win 'resize.

insert-event-func

To be operational, your code must define a custom event handler that  handles the resize of window content when resized event occurs:

insert-event-func [
if event/type = 'resize [
win/pane/1/size: win/size - 20x20
'done
]
]

Then, it's really simple to update image size with the window/pane facet which handles the faces included in your window. In the following example, main window contains only one image face numbered 1.

Code example

Red [
    Title:   "Red Computer Vision: Resize"
    Author:  "Francois Jouen"
    File:    %resize.red
    Needs:   'View
]


; use the image you want
img1: load %lena.jpg
;creates window title
title: "Resizes "
append title form to-local-file %lena.jpg

; main windows with resize flag

win: layout [
        title title     ; window title
        image img1      ; an image face for displaying loaded image
]

view/flags/no-wait win 'resize
; A custom event handler 
; This handles the resize of window content when resized

insert-event-func [
    if event/type = 'resize [
        win/pane/1/size: win/size - 20x20
        'done
    ]
]

do-events


And the result for down and up sizing











Aucun commentaire:

Enregistrer un commentaire