If SFML allowed the image to be created in its constructor, it may be a lot nicer
Looks like it could be done (the create function has no return value).
Might be useful for temporary images as I demonstrated above but I'm not sure how common this would be to warrant creating a new constructor just so that is can also call create.
That version does remove some verbosity but I don't think that was the problem. The problem really is that the temporary image isn't very temporary. Obviously, it could be contained in a self-created function to fix this. However, it can, of course, be contained inline to limit its scope very simply (in case your only real problem is that the temporary image outlives its usefulness:
sf::Image image
image.create(100, 100, sf::Color::Black);
{
sf::Image tempRectImage;
tempRectImage.create(40, 40, sf::Color::Red);
image.copy(tempRectImage, 20, 20);
} // tempRectImage is destroyed here :)
So sorry about my habit to prefer the solutions from box
Create a simple function that fills the rectangle for you
Remember that you can do these sorts of things easily with render textures and rectangle shapes:
sf::RenderTexture renderTexture;
renderTexture.create(100, 100);
renderTexture.clear(sf::Color::Black);
{
sf::RectangleShape rectangle({ 60, 60 });
rectangle.setFillColor(sf::Color::Red);
rectangle.setPosition({ 20, 20 });
renderTexture.draw(rectangle);
} // rectangle is destroyed here
renderTexture.display();
It opens up a lot more options of how to draw to the "image" when it is a render texture (you can use and re-use any SFML drawable, for example).
If you still need to have this as an sf::Image, you can then convert it from a render texture when you're finished:
sf::Image image{ renderTexture.getTexture().copyToImage() };