Fix #9599: Preserve preview mode when selected_index exceeds the number of images in the gallery. (#10816)
* Fix #9599: Preserve Selected Index in Gallery Component
Fixed preview mode unexpected exit when selected_index is greater
than the number of images in the gallery.
* Fix #9599: Prevent preview exit when index exceeds image count
Updated the index selection process so that the slider selects the last
image if the chosen index is out of bounds, instead of exiting preview
mode unexpectedly.