一个QImage或QPixmap新建变量时,第一次填充图片时设置的宽高正常(fill),其后的绘制操作(draw)则会在绘制的矩形底边和右边加上painter.pen().width()
在下面代码17行设置端点查看内存可以直观看到只在右边和底边++
1 // 1. 生成掩码图 2 const int width = 32; 3 const int height = 20; 4 QImage maskImg(width, height, QImage::Format_Mono); 5 QPainter maskPainter(&maskImg); 6 // 7 maskPainter.setPen(QPen(Qt::white, 0)); 8 maskPainter.setBrush(Qt::white); 9 maskPainter.drawRect(0, 0, width, height); 10 maskPainter.setPen(QPen(Qt::black, 0)); 11 maskPainter.setBrush(Qt::black); 12 maskPainter.drawRect(16, -16, width, height); 13 maskPainter.setPen(QPen(Qt::black, 0)); 14 maskPainter.setBrush(Qt::black); 15 maskPainter.drawRect(16, 16, 2, 3); 16 17 maskImg.save("/home/lt47/Desktop/ORIGIN.jpg"); 18 qDebug() << maskImg.byteCount(); 19 qDebug() << maskImg.bytesPerLine(); 20 QRgb b = maskImg.pixel(16,20); 21 qDebug() << qRed(b) << qGreen(b) << qBlue(b); 22 QRgb ww = maskImg.pixel(15,20); 23 qDebug() << qRed(ww) << qGreen(ww) << qBlue(ww);