天天看點

Qt之九宮格圖檔處理

 /*

**  功能             : 9格圖處理

**  picName          : 圖檔名字

**  titleHeight      : 标題欄高度(機關:像素)

**  BorderLineWidth  : 邊框寬度(機關:像素)

**  parent           : 帶畫背景的視窗句柄

**  傳回值              :處理完成的圖檔

*/

QPixmap* NinePatch(QString& picName,int iHorzSplit,int iVertSplit, int DstWidth, int DstHeight)

{

    QPixmap* pix = new QPixmap(picName);

    int pixWidth = pix->width();

    int pixHeight = pix->height();

    QPixmap pix_1 = pix->copy(0, 0, iHorzSplit, iVertSplit);

    QPixmap pix_2 = pix->copy(iHorzSplit, 0, pixWidth-iHorzSplit*2, iVertSplit);

    QPixmap pix_3 = pix->copy(pixWidth-iHorzSplit, 0, iHorzSplit, iVertSplit);

    QPixmap pix_4 = pix->copy(0, iVertSplit, iHorzSplit, pixHeight-iVertSplit*2);

    QPixmap pix_5 = pix->copy(iHorzSplit, iVertSplit, pixWidth-iHorzSplit*2, pixHeight-iVertSplit*2);

    QPixmap pix_6 = pix->copy(pixWidth-iHorzSplit, iVertSplit, iHorzSplit, pixHeight-iVertSplit*2);

    QPixmap pix_7 = pix->copy(0, pixHeight-iVertSplit, iHorzSplit, iVertSplit);

    QPixmap pix_8 = pix->copy(iHorzSplit, pixHeight-iVertSplit, pixWidth-iHorzSplit*2, pixWidth - iHorzSplit*2);

    QPixmap pix_9 = pix->copy(pixWidth-iHorzSplit, pixHeight-iVertSplit, iHorzSplit, iVertSplit);

    pix_2 = pix_2.scaled(DstWidth-iHorzSplit*2,iVertSplit, Qt::IgnoreAspectRatio);//保持高度拉寬

    pix_4 = pix_4.scaled(iHorzSplit, DstHeight-iVertSplit*2, Qt::IgnoreAspectRatio);//保持寬度拉高

    pix_5 = pix_5.scaled(DstWidth-iHorzSplit*2,DstHeight-iVertSplit*2, Qt::IgnoreAspectRatio);

    pix_6 = pix_6.scaled(iHorzSplit, DstHeight-iVertSplit*2, Qt::IgnoreAspectRatio);//保持寬度拉高

    pix_8 = pix_8.scaled(DstWidth-iHorzSplit*2, iVertSplit);//保持高度拉寬

    QPixmap* resultImg =new QPixmap(DstWidth, DstHeight);

    QPainter* painter = new QPainter(resultImg);

    if (!resultImg->isNull()) {

        painter->drawPixmap(0,0,pix_1);

        painter->drawPixmap(iHorzSplit, 0, pix_2);

        painter->drawPixmap(DstWidth-iHorzSplit,0,pix_3);

        painter->drawPixmap(0, iVertSplit, pix_4);

        painter->drawPixmap(iHorzSplit, iVertSplit, pix_5);

        painter->drawPixmap(DstWidth-iHorzSplit, iVertSplit, pix_6);

        painter->drawPixmap(0, DstHeight-iVertSplit, pix_7);

        painter->drawPixmap(iHorzSplit, DstHeight-iVertSplit, pix_8);

        painter->drawPixmap(DstWidth-iHorzSplit, DstHeight-iVertSplit, pix_9);

        painter->end();

    }

    return resultImg;

}