翻转/镜像QImage的正确方法是什么?以下代码段无效。
//allocate buffer
BYTE* pRgb32Buffer = new BYTE[width*height* 4];
//create paint device
QImage img = QImage(pRgb32Buffer , width, height, getStride(width, pixelFormat), QImage::Format_RGB32);
//do some drawing on image (works!)
QPainter painter(&img);
painter.drawText(10, 50, QString("some text"));
//mirrore image (doesn't mirror the orignal buffer!!!)
img = img.mirrored(false,true);
//doesn't work either
//QImage mirrored = img.mirrored();
//img = mirrored;
//mirrored.detach();
使用QImage::mirrored()
:的解决方案
memmove(pRgb32Buffer, img.mirrored().bits(),img.byteCount());
我有这个工作代码来将QPixmap镜像到QImage上。
QPixmap* source = //... Getting my pixmap form somewhere...
QImage target(QSize(source->width(), source->height()), QImage::Format_ARGB32);
QPainter painter(&target);
QTransform transf = painter.transform();
transf.scale(-1, 1);
painter.setTransform(transf);
painter.drawPixmap(-source->width(), 0, *source);
source
包含该代码之后的镜像像素图。您应该能够对QImage
和QPainter::drawImage
函数执行同样的操作。
如果你想要的话,你也可以保存文件(确保你有imageformats dll,否则它不会写):
QImageWriter writer("c:\theimage.tiff", "tiff");
writer.setCompression(1);
writer.write(target);