How do you save a programmaticly created texture to a file

Thanks. that was the great solution but what if is use draw material instead of Draw texture to draw from my material? It’s giving me blank image don’t know why?
image