Give this a go…
class Slice9 : public Component
{
public:
Slice9(){}
~Slice9(){}
// the width and height of the slice rectangle are actually the right and bottom boundries, not width & height
void setImage(Image* theImage, Rectangle<int>theSlice)
{
jassert(theSlice.getWidth() < theImage->getWidth());
jassert(theSlice.getHeight() < theImage->getHeight());
image = theImage;
slice = theSlice;
}
void paint(Graphics& g) override
{
if(image != nullptr)
{
const int dw = getWidth();
const int dh = getHeight();
const int iw = image->getWidth();
const int ih = image->getHeight();
const int sliceLeft = slice.getX();
const int sliceTop = slice.getY();
const int sliceRight = slice.getWidth();
const int sliceBottom = slice.getHeight();
const int sCenterWidth = iw - sliceLeft - (iw - sliceRight);
const int sCenterHeight = ih - sliceTop - (ih - sliceBottom);
const int dRightX = dw - (iw - sliceRight);
const int dBottomY = dh - (ih - sliceBottom);
const int dCenterHeight = dh - sliceTop - (ih - sliceBottom);
const int dCenterWidth = dw - sliceLeft - (iw - sliceRight);
const int bottomHeight = ih - sliceBottom;
const int rightWidth = iw - sliceRight;
// left column
g.drawImage(*image, 0, 0, sliceLeft, sliceTop, 0, 0, sliceLeft, sliceTop);
g.drawImage(*image, 0, sliceTop, sliceLeft, dCenterHeight, 0, sliceTop, sliceLeft, sCenterHeight);
g.drawImage(*image, 0, dBottomY, sliceLeft, bottomHeight, 0, sliceBottom, sliceLeft, bottomHeight);
// center column
g.drawImage(*image, sliceLeft, 0, dCenterWidth, sliceTop, sliceLeft, 0, sCenterWidth, sliceTop);
g.drawImage(*image, sliceLeft, sliceTop, dCenterWidth, dCenterHeight, sliceLeft, sliceTop, sCenterWidth, sCenterHeight);
g.drawImage(*image, sliceLeft, dBottomY, dCenterWidth, bottomHeight, sliceLeft, sliceBottom, sCenterWidth, bottomHeight);
// right column
g.drawImage(*image, dRightX, 0, rightWidth, sliceTop, sliceRight, 0, rightWidth, sliceTop);
g.drawImage(*image, dRightX, sliceTop, rightWidth, dCenterHeight, sliceRight, sliceTop, rightWidth, sCenterHeight);
g.drawImage(*image, dRightX, dBottomY, rightWidth, bottomHeight, sliceRight, sliceBottom, rightWidth, bottomHeight);
}
}
private:
Image* image = nullptr;
Rectangle<int>slice;
};
Here’s the setup:
{
buttonImage = ImageCache::getFromMemory(BinaryData::button_png, BinaryData::button_pngSize);
Rectangle<int>buttonSlice(12, 12, 46, 46);
original.setImage(buttonImage);
original.setBounds(10, 10, 58, 58);
addAndMakeVisible(original);
tall.setImage(&buttonImage, buttonSlice);
tall.setBounds(500, 100, 80, 350);
addAndMakeVisible(tall);
wide.setImage(&buttonImage, buttonSlice);
wide.setBounds(100, 500, 350, 80);
addAndMakeVisible(wide);
big.setImage(&buttonImage, buttonSlice);
big.setBounds(100, 100, 350, 350);
addAndMakeVisible(big);
}
And here’s a test…the top left image is the original image as an ImageComponent, the other 3 stretched images are the Slice9s.