material thumbnails code

any one know how to get the code for that little icon ?
i want to make an addon that will give you a big grid of them so you preview all your mats and categorize them (just making it for fun)
trying to find the code is just leading me to dead ends

or an off screen preview render that can be saved and used as an icon would be cool to but i cant find how to save the preview image other than by using a screenshot but that will not work on off screen stuff