关于QT图片处理(九宫格图片缩放问题)

/*

**  功能                : 9格图处理

**  picName          : 图片名字

**  titleHeight      : 标题栏高度(单位:像素)

**  BorderLineWidth  : 边框宽度(单位:像素)

**  parent           : 带画背景的窗口句柄

**  返回值              :处理完成的图片

*/

QPixmap* NinePatch(QString& picName,int titleHeight,int BorderLineWidth ,QWidget* Wnd)

{

    QPixmap* pix = new QPixmap(picName);

    

    int pixWidth = pix->width();

    int pixHeight = pix->height();

    

    QSize WndSize = Wnd->size();

    int DstWidth = WndSize.width();

    int DstHeight = WndSize.height();

    

    QPixmap pix_1 = pix->copy(0,0,BorderLineWidth,titleHeight);

    QPixmap pix_2 = pix->copy(BorderLineWidth,0,pixWidth-BorderLineWidth*2,titleHeight);

    QPixmap pix_3 = pix->copy(pixWidth-BorderLineWidth,0,BorderLineWidth,titleHeight);

    

    QPixmap pix_4 = pix->copy(0,titleHeight,BorderLineWidth,pixHeight-titleHeight-BorderLineWidth);

    QPixmap pix_5 = pix->copy(BorderLineWidth,titleHeight,pixWidth-BorderLineWidth*2,pixHeight-titleHeight-BorderLineWidth);

    QPixmap pix_6 = pix->copy(pixWidth-BorderLineWidth,titleHeight,BorderLineWidth,pixHeight-titleHeight-BorderLineWidth);

    

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

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

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

    

    pix_2 = pix_2.scaled(DstWidth-BorderLineWidth*2,titleHeight,Qt::IgnoreAspectRatio);//保持高度拉宽

    pix_4 = pix_4.scaled(BorderLineWidth,DstHeight-titleHeight-BorderLineWidth,Qt::IgnoreAspectRatio);//保持宽度拉高

    pix_5 = pix_5.scaled(DstWidth-3,DstHeight-titleHeight-BorderLineWidth);

    pix_6 = pix_6.scaled(BorderLineWidth,DstHeight-titleHeight-BorderLineWidth,Qt::IgnoreAspectRatio);//保持宽度拉高

    pix_8 = pix_8.scaled(DstWidth-BorderLineWidth*2,BorderLineWidth);//保持高度拉宽

    

    

    QPixmap* resultImg =new QPixmap(WndSize);

    QPainter* painter = new QPainter(resultImg);

    if (!resultImg->isNull())

    {

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

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

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

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

        painter->drawPixmap(3,titleHeight,pix_5);

        painter->drawPixmap(DstWidth-3,titleHeight,pix_6);

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

        painter->drawPixmap(3,DstHeight-3,pix_8);

        painter->drawPixmap(DstWidth-3,DstHeight-3,pix_9);

        painter->end();

    }

    return resultImg;

}

http://bbs.csdn.net/topics/390506636

    原文作者:九宫格问题
    原文地址: https://blog.csdn.net/zerokkqq/article/details/53142920
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞